使用嵌套资源在模态中编辑表单

时间:2014-04-29 23:45:36

标签: ruby-on-rails ruby ruby-on-rails-4 renderpartial nested-resources

在我的SCREEN节目视图中,我会渲染评论

<%= 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">&times;</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="&#x2713;" />
  <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="&#x2713;" />
  <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

CommentsController:

  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

1 个答案:

答案 0 :(得分:1)

您要将所有编辑链接发送到包含id = EditComment的HTML元素。您的所有模态都具有相同的id,即EditComment。因此,它将始终选择第一个模态,因为它将是第一个匹配。

您需要更改模式的主div,以便为不同的id's设置不同的comments。例如:

<div class="modal fade" id="EditComment_<%= comment.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

然后更新您的link,如下所示

<%= link_to "Edit", "#EditComment_#{comment.id}", 'data-toggle' => 'modal' %>
相关问题