render用法

render paratial: '渲染当前页所用到的的html的文件路径', locals: {subject: @subject}

render第二个参数是将主图片的参数变量@subject传递到渲染的局部图片中可以用subject来引用


实际项目:

1.locals: {subject: @subject}用来向局部视图传递变量

_form.html.erb中:

<%= render :partial => '/manage/subjects/show_content', :locals => {subject: @subject} %>

_show_content.html.erb中:

<% if subject.body.present? %>     
  <table class="ui celled striped table">
  <thead>
  <tr>
    <th>Type</th>
    <th>Content</th>
    <th>Operation</th>
  </tr>
  </thead>  
    <% subject.body.each_with_index do |s, index| %>
      <% if s[:type] == 'text' %>
        <tr>
          <td><%= s[:type] %></td>
          <td><%= s[:content] %></td>
          <td>
          <%= link_to 'edit', manage_edit_content_path(subject, index), remote: true %> | 
          <%= link_to 'delete', manage_content_delete_path(subject, index), remote: true, :onclick => "alert('Are you sure?')" %>
          </td>
        </tr>
      <% elsif s[:type] == 'photo' %>
        <tr>
          <td><%= s[:type] %></td>
          <td><%= image_tag(s[:medium_url]) if s[:medium_url] %></td>
          <td>
          <%= link_to 'edit', manage_edit_content_path(subject, index), remote: true %> | 
          <%= link_to 'delete', manage_content_delete_path(subject, index), remote: true, :onclick => "alert('Are you sure?')" %>
          </td>
        </tr>
      <% end %>
<% end %>
  </table>
<% end %>

################在_show_content.html.erb中对_form.html.erb中的@subject进行引用变为subject##########


2.locals: {room: @room}这里局部视图引用的是controller中的变量@room,而room只是起个别名,防止多个视图引用同一个局部视图 

index.html.erb中:

<%= link_to "update Subject", manage_add_subject_to_room_path(room), remote: true, class: "ui" %>

room_controller.rb中:

  def add_subject_to_room
    @room = Room.find_by(number: params[:id])
    @subjects = Subject.all
  end

add_subject_to_room.js.erb中:

$("#placeholder").html('<%= j(render(partial: "manage/rooms/subject", locals: {room: @room})) %>');
$("#placeholder-modal").modal("show");
$('.ui.checkbox').checkbox();

_subject.html.erb中:

<div class="ui modal" id="placeholder-modal">
  <div class="header">Choose room's subject</div>
    <div class="content">
      <div class="ui form">
        <%= form_tag  "/manage/rooms/update_subject/#{@room.id}", remote: true, class: "ui form" do %>
          <% @subjects.each_slice(3) do |items| %>
            <div class="fields three">
              <% items.each do |subject| %>
                <div class="field">
                <%= subject.id %>.
                  <div class="ui checkbox">
                    <input type="checkbox" class="hidden" name="room[subject_ids][]" value="<%= subject.id %>" <%= 'checked' if subject.include_room?(room) %>/>
                    <label><%= subject.title %></label>
                  </div>
                </div>
              <% end %>
            </div>
          <% end %>
#####################在_subject.html.erb中@subject引用的是controller中的变量@subject, 而不是render中locals参数传递的变量,该传递变量只起到起别名的作用

          <div class="field">
            <%= submit_tag "approve", class: "ui button" %>
          </div>
        <% end %>
      </div>
    </div>
  </div>
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值