【发布时间】:2014-04-29 23:45:35
【问题描述】:
在我的 SCREEN 显示视图中,我渲染了 cmets
<%= render @screen.comments.order("created_at DESC") %>
该部分包含注释和一个编辑链接,该链接指向也在该部分中的模式。
<%= link_to "Edit", '#EditComment', 'data-toggle' => 'modal' %>
模态看起来像这样:
<!-- Comment Edit Modal -->
<% if can? :update, Comment %>
<div class="modal fade" id="EditComment" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog edit_scr_modal">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Edit your Comment</h4>
</div>
<%= form_for [@screen, comment] do |f| %>
<div class="modal-body">
<div class="form descr_area">
<label>Comment:</label><br>
<%= f.text_area :body, :rows => "3" %>
</div>
</div>
<div class="modal-footer">
<%= button_tag(type: "submit", class: "btn") do %>
<i class="fa fa-check"></i> Edit Comment
<% end %>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
问题来了。
虽然由此输出的 Html 是针对 2 个不同的评论 ID(2 个不同的评论):
<form accept-charset="UTF-8" action="/s/7-testing-one-two-three/comments/20" class="edit_comment" id="edit_comment_20" method="post">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="patch" />
<input name="authenticity_token" type="hidden" value="0wcpual8qoJ1lzktwbNFPRikq/Cq4WCnyXi8IjIDitc=" />
</div>
和
<form accept-charset="UTF-8" action="/s/7-testing-one-two-three/comments/11" class="edit_comment" id="edit_comment_11" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="patch" /><input name="authenticity_token" type="hidden" value="0wcpual8qoJ1lzktwbNFPRikq/Cq4WCnyXi8IjIDitc=" />
</div>
点击编辑链接后,它会显示带有最新评论的模态(created_at DESC),如果我点击另一个评论,它只会再次显示第一个评论。
如果我创建一个新评论,它将再次出现在所有模式中。
所以我认为它与传递 .order(created_at DESC) 时的部分渲染有关...
我在这里错过了什么??
路线:
resources :screens, :path => 's' do
resources :comments
end
评论控制器:
def edit
@screen = Screen.find(params[:screen_id])
@comment = current_user.comments.find(params[:id])
end
def create
@screen = Screen.find(params[:screen_id])
@comment = current_user.comments.build(comment_params)
@comment.screen_id = @screen.id
respond_to do |format|
if @comment.save
@comment.create_activity :create, owner: current_user, recipient: @comment.screen.user
format.html { redirect_to @screen, notice: 'Comment was successfully created.' }
else
format.html { redirect_to @screen }
end
end
end
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment.screen, notice: 'Comment was successfully updated.' }
else
format.html { redirect_to @screen }
end
end
end
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 renderpartial nested-resources