简体   繁体   English

Rails 4 JavaScript / JQuery通过编辑控制器动作呈现多个局部

[英]Rails 4 JavaScript / JQuery render multiple partial through edit controller action

I am trying to get multiple partials on the same index view with the same edit controller action. 我正在尝试使用相同的编辑控制器操作在同一索引视图上获取多个局部。

Basically i have a list of both ID's and of shipping quantity's i would like the user to enter. 基本上,我希望用户输入ID和运输数量的列表。 The partials have both of the form inputs with an update button. 部件具有带有更新按钮的两种形式输入。 However, when i try do use the partials as follows no matter which "Edit" link i click both partials show up regardless of which js ID i have set for the table column. 但是,当我尝试按以下方式使用局部变量时,无论我为表列设置了哪个js ID,无论单击哪个“编辑”链接,我都将同时显示两个局部变量。

My edit action in controller: 我在控制器中的编辑操作:

def edit
  @invoice = Invoice.find(params[:id])
   respond_to do |format|
    format.js
    format.html
    end
 end

Part of my index view: 我的索引视图的一部分:

<% @invoices.each do |invoice| %>
  <td id="packlist"><%= invoice.readpacklistid %><%= link_to 'Edit', edit_invoice_path(invoice), :remote => true %></td>
  <td id="shipqty"><%= invoice.shippedqty %><%= link_to 'Edit', edit_invoice_path(invoice), :remote => true %></td>
<% end %>

My edit.js.erb contains: 我的edit.js.erb包含:

$('#shipqty').append( "<%= escape_javascript( render(:partial => 'shippedqty', :locals => {'invoice' => @invoice}) ) %>");
$("#packlist").append( "<%= escape_javascript( render(:partial => 'packlist', :locals => {'invoice' => @invoice}) ) %>");

_shippedqty.html.erb partials contains: _shippedqty.html.erb部分包含:

<%= form_for(@invoice) do |f| %>
  <%= f.label :shippedqty %>
  <%= f.select :shippedqty, options_for_invoices(@invoice.customerorderid) %>
  <%= f.submit %>
  <%= link_to "Cancel", invoices_path %>
<% end %>

_packlist.html.erb _packlist.html.erb

<%= form_for(@invoice) do |f| %>
  <%= f.label :packlistid %>
  <%= f.select :packlistid, options_for_packlistid(@invoice.customerorderid, @invoice.shippedqty), :selected => :packlistid %>
  <%= f.submit %>
<% end %>

I would like both of these partials to show up on the same page but show up one at a time when clicked on their desired link. 我希望这两个部分都显示在同一页面上,但单击它们所需的链接时一次显示一个。 I just find it much more beneficial to the user than having to click a link to edit these all at once. 我发现对用户而言,它比单击链接一次编辑所有内容更为有益。

This is using Rails 4.1.8 and Ruby 2.1.0p0 这是使用Rails 4.1.8和Ruby 2.1.0p0

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks! 谢谢!

I finally figured out a way to do this which makes it easy. 我终于想出了一种简便的方法。

Invoices.helper Invoices.helper

def options_for_shippedqty(customerid)
  Shipperline.getshippqty(customerid)
end

#Used to get all the packlistid for a invoice record for drop down select

def options_for_packlistid(customerid, shipqty)
 Shipperline.packlist(customerid, shipqty)
end

Then in my view 那在我看来

<%= form_for invoice do |f| %>
   <% @packlistFromList = options_for_packlistid(invoice.customerID,     invoice.shippedqty).first %>
 <% if @packlistFromList.nil? %>
   <td id="putcolor"><%= f.text_field :packlistid %><%= f.submit "Update  Packlist", { :class => "btn btn-primary" } %>
 <% else %>
   <td id="putcolor"><%= f.select :packlistid, options_for_packlistid(invoice.customerID, invoice.shippedqty) %><%= f.submit  "Update Packlist", { :class => "btn btn-primary"} %></td>
 <% end %>
  <% @shipQty = options_for_shippedqty(invoice.customerID).first %>
 <% if @shipQty.nil? %>
  <td id="putcolor"><%= f.text_field :shippedqty %><%= f.submit "Update Shipping QTY", { :class => "btn btn-primary" } %>
 <% else %>
  <td id="putcolor"><%= f.select :shippedqty, options_for_shippedqty(invoice.customerID) %><%= f.submit "Update Shipping QTY", { :class => "btn btn-primary"} %></td>
 <% end %>
  <td id="putcolor"><%= invoice.adder %></td>
  <td id="putcolor"><%= invoice.adderprice %></td>
  <td id="putcolor"><%= invoice.difference %></td>
  <td id="putcolor"><%= f.text_area :comments %><%= f.submit "Add Comment",  { :class => "btn btn-primary"} %></td>
  <td id="putcolor"><%= f.check_box :completed %><%= f.submit "Submit   Completion", { :class => "btn btn-primary"} %></td>
  <td class="hidden"><%= invoice.orgShippedQty %></td>
  <td class="hidden"><%= invoice.getPrecMetalAmount %></td>
  <td class="hidden"><%= invoice.orgPackList %>
  <td class="hidden"><%= invoice.invoiceNum %>
 </tr>
</tbody>
<% end %>
<% end %>

I porpouse you, render Partial from AJAX, I explain myself: 我请你,从AJAX渲染Partial,我向自己解释:

  • When a user click on edit, you have to make a ajax call to an action edit that correspond. 用户单击编辑时,必须对相应的动作编辑进行ajax调用。
  • This actions should return a html code (the form). 此操作应返回一个html代码(窗体)。
  • Append this html code where you want in success param of AJAX. 将此HTML代码附加到AJAX成功参数中所需的位置。 (use $jquery.html) (使用$ jquery.html)
  • Delete de form after use it. 使用后删除表格。

Or 要么

<% @invoices.each do |invoice| %>
  <td id="'packlist'+@invoice.id"><%= invoice.readpacklistid %><%= link_to 'Edit', edit_invoice_path(invoice), :remote => true %></td>
  <td id="'shipqty'+@invoice.id"><%= invoice.shippedqty %><%= link_to 'Edit', edit_invoice_path(invoice), :remote => true %></td>
<% end %>

and

$('#shipqty+@invoice.id').append( "<%= escape_javascript( render(:partial => 'shippedqty', :locals => {'invoice' => @invoice}) ) %>");
$("#packlist+@invoice.id").append( "<%= escape_javascript( render(:partial => 'packlist', :locals => {'invoice' => @invoice}) ) %>");

In your edit.js.erb file you are rendering both file that is why your getting both from at the same time. 在您的edit.js.erb文件中,您要渲染两个文件,这就是为什么要同时获取两个文件的原因。
Even you are not able to figure out in your edit action which link was clicked as you are not sending anything to segregate both links. 甚至您也无法在编辑操作中确定单击了哪个链接,因为您没有发送任何东西来分隔两个链接。

you need to send some identifier in params whenever a user click on any of these links and then in edit.js.erb use if/else based on these params condition, to figure out which file needs to render. 每当用户单击这些链接中的任何一个时,您都需要在参数中发送一些标识符,然后在edit.js.erb中根据这些参数条件使用if / else来确定需要渲染的文件。

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

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