简体   繁体   English

使用Ajax编辑表行的正确方法是什么?

[英]What is the correct way to use Ajax for editing table rows?

I looked through a number of posts with similar titles but have not found an answer. 我浏览了许多标题相似的帖子,但没有找到答案。

In my application I want to allow editing Active records from an index view. 在我的应用程序中,我希望允许从索引视图编辑活动记录。 This view uses link_to_remote for each row. 该视图为每一行使用link_to_remote。 When user presses the link the row should be replaced with a form that user may submit via Ajax. 当用户按下链接时,该行应替换为用户可以通过Ajax提交的表单。

Table is displayed by sending a collection to show_item partial 通过将集合发送到show_item部分来显示表

<% content_tag_for(:tr, show_item) do %>
<td><%=h show_item.name %> </td>
<td><%= show_item.role %></td>
<td><%= link_to_remote 'Edit', :url => edit_participation_path(show_item), :method => :get %> </td>
<% end %>

My controller implements editing as 我的控制器将

   def edit
     @participation = Participation.find(params[:id])
     respond_to do |format|
       format.js do
         render(:update) { |page| page[@participation].replace_html(:partial => "edit_item", :object => @participation ) }
       end
     end
    end

Edit_item partial looks like Edit_item部分看起来像

<% content_tag_for(:tr, edit_item) do %>
  <% form_remote_for edit_item do |f| %>
     <td><%= edit_item.name %></td>
     <td> <%= f.select(:role, options_for_select(all_roles, edit_item.role)) %> </td>
     <td><%= submit_tag 'Update', :class => 'submit' %></td>
     <td><%= link_to_remote 'Cancel', :url => participation_path(edit_item) %></td>
   <% end %>
 <% end %>

Remote calls do go through but they affect the table in an unexpected way. 确实会进行远程调用,但是它们以意外方式影响表。 In Firefox pressing the link causes the row to disappear. 在Firefox中,按下链接会使该行消失。 On the other hand if I use Safari the row is updated but instead of aligning cells with other table rows the entire replaced row is placed to the left of other rows. 另一方面,如果我使用Safari,则该行会更新,但不是将单元格与其他表行对齐,而是将整个替换行放置在其他行的左侧。

Any ideas? 有任何想法吗?

First thing that comes to mind is a syntax error in the html. 首先想到的是html中的语法错误。 I notice you have 4 td tags in your edit partial, but only 3 in the show partial. 我注意到您在编辑部分中有4个td标签,但在显示部分中只有3个td标签。 You may want to view the generated source from Firebug and validate it. 您可能需要查看Firebug生成的源并进行验证。

Are you using any template? 您正在使用任何模板吗? when you use link_to_remote it loads the result with a template if you are using one. 当您使用link_to_remote时,如果使用模板,它将使用模板加载结果。

Check the source code using firebug addon and confirm if the code in the result is exactly what you want. 使用firebug插件检查源代码,并确认结果中的代码是否正是您想要的。

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

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