简体   繁体   English

Rails Form_for在许多对象的模式下

[英]Rails Form_for in modal for many objects

If there's a better way to phrase the question, or a better way to do this, please let me know. 如果有更好的方法来表达问题,或者有更好的方法来解决这个问题,请告诉我。

I want to update some models using the form_for using a modal. 我想使用模态使用form_for更新一些模型。 User clicks on an "edit" and a modal pop-up with the fields and can click submit or cancel. 用户单击带有字段的“编辑”和模式弹出窗口,然后可以单击提交或取消。 One way I can do it is create a modal for every single model, but that seems wrong and would really bulk up the html.erb file. 我可以做到的一种方法是为每个模型创建一个模态,但这似乎是错误的,并且确实会堆积html.erb文件。

How can I do this? 我怎样才能做到这一点? I'm guessing I use the remote feature somehow? 我猜我以某种方式使用了远程功能?

<div class="modal fade" id="edit-modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">             
          <%= simple_form_for(@rate) do |f| %>
            <%= f.label "Rate / session" %> 

            <%= f.input :rate_dollars, :label => false, collection: 0..100, :selected => @dol_amt, :include_blank => false %>
            <%= f.input :rate_cents, :label => false, collection: {".00" => 0, ".25" => 0.25, ".50" => 0.50, ".75" => 0.75}, :selected => @cent_amt,  :include_blank => false %>        

            <%= f.hidden_field :uid, :value => @user.id %>
            <%= f.submit "Update", class: "btn btn-primary" %>
          <% end %>         
        </div>
    </div>
</div>

The way I got it accomplished was setting up a modal skeleton in the index page, and then a bit of JS to load the form with the controller action responding to format.js with the form partial. 我完成它的方法是在索引页中设置一个模态框架,然后再用一些JS来加载带有控制器动作的表格,该动作响应带有部分表格的format.js

<div id="user-form-edit" class="modal fade">
  <div class="modal-dialog modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      <h3>Edit User</h3>
    </div>
    <div class="modal-body"></div>
  </div>
</div>

Then in a JS file: 然后在一个JS文件中:

$('#user-form-edit').on("show.bs.modal", function(e) {
    $(this).find('.modal-body').load(e.relatedTarget.dataset.url);
});

In the controller: 在控制器中:

def edit
  @user = User.find(params[:id])
  respond_to do |format|
    format.js { render partial: "form", locals: {user: @user}}
  end
end

This way, you have a single modal that you can use for all the users. 这样,您就可以为所有用户使用单个模式。

-- EDIT how to open the modal --- -编辑如何打开模态-

<%= link_to "#", class: "btn btn-warning edit",
    data: {
      toggle: "modal",
      url: edit_admin_user_path(user.id),
      target: "#user-form-edit"} do %>
  <i class="glyphicon glyphicon-edit glyphicon-white"></i>
  Edit
<% end %>

You are on the right track by thinking about using the remote feature (AJAX) for what you want to achieve. 通过考虑将远程功能(AJAX)用于您要实现的目标,您处于正确的轨道。

First just create an empty div that will be populated eventually with an AJAX query: 首先,只需创建一个空div,最后将使用AJAX查询填充该div:

<div id="edit-modal" class="modal fade" role="dialog"></div>

Next change your edit to hit an action in your controller using AJAX by adding remote: true to the link and associate it with your modal dialog using data-toggle and data-target. 接下来,通过向链接添加remote:true来更改您的编辑以使用AJAX在控制器中执行操作,并使用数据切换和数据目标将其与模式对话框关联。

  <%= link_to edit_modelinstance(@modelinstance), remote: true, 'data-toggle' => 'modal', 'data-target' => '#edit-modal' do %>

then create a partial that will contain the modal content to be added to the empty div created earlier .. eg, _edit.html.erb 然后创建一个部分,它将包含要添加到先前创建的空div中的模态内容。例如,_edit.html.erb

    <div class="modal-dialog">
      <div class="modal-content">

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h3 id="myModalLabel">Modal Title</h3>
        </div>

        <div class="modal-body">
          <%= simple_form_for(@rate) do |f| %>
            <%= f.label "Rate / session" %> 

            <%= f.input :rate_dollars, :label => false, collection: 0..100, :selected => @dol_amt, :include_blank => false %>
            <%= f.input :rate_cents, :label => false, collection: {".00" => 0, ".25" => 0.25, ".50" => 0.50, ".75" => 0.75}, :selected => @cent_amt,  :include_blank => false %>        

            <%= f.hidden_field :uid, :value => @user.id %>
            <%= f.submit "Update", class: "btn btn-primary" %>
          <% end %>          

          <div class="modal-footer">
            <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <%= f.submit 'Submit', class: 'btn btn-primary' %>
          </div>
        </div>
      </div>
    </div>

Note the div structure (those parts identified by class of modal-xxxx) is important for the modal to display correctly. 请注意,div结构(由modal-xxxx类标识的那些部分)对于正确显示模式非常重要。

Now finally you just need to render and populate the div as a response to the AJAX call so create an js.erb (eg, edit.js.erb) file for your controllers action and add the following to it. 现在,最后您只需要渲染和填充div作为对AJAX调用的响应,因此为控制器操作创建一个js.erb(例如edit.js.erb)文件,并将以下内容添加到其中。

$("#edit-modal").html("<%= escape_javascript(render partial: 'form') %>");

Finally just ensure you are rendering js from your controller: 最后,只需确保您正在从控制器渲染js:

  format.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM