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>