【问题标题】:Edit Form in a Modal with nested resources在具有嵌套资源的模式中编辑表单
【发布时间】: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">&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

评论控制器:

  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


    【解决方案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' %>
    

    【讨论】:

    • 天哪...现在你指出来太合乎逻辑了:) 谢谢 Kirti。
    • 很高兴再次为您提供帮助:D
    猜你喜欢
    • 2014-08-02
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多