Rails 3 fields_for-排序顺序丢失 [英] Rails 3 fields_for - sort order gets lost

查看:94
本文介绍了Rails 3 fields_for-排序顺序丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Rails 3.0.3与ruby 1.9.2p0一起使用.

I am using Rails 3.0.3 with ruby 1.9.2p0.

在我的profiles_controller(编辑功能)中,我有这个呼叫

In my profiles_controller (edit function) I have this call

@profile = Profile.find(params[:id])
@profile_items = @profile.profile_items.order("pos")

以正确的顺序获取@profile_items,并在"pos"上排序. 在_form.html.erb中,我有以下内容

to get the @profile_items in the correct order, sorted on "pos". In the _form.html.erb I have the following

<% @profile_items.each do |pi| %>
  <%= pi.pos %> | 
<% end %>
<%= f.fields_for :profile_items do |f2| %>
  <%= render 'profile_item_fields', :f => f2 %>
<% end %>

前三行是测试代码,用于显示@profile_items的顺序正确. 但是当它们被渲染时,它们已经失去了排序顺序!

The 3 first lines are test code to show that the @profile_items are in the correct order. But when they are rendered they have lost the sorted order!

现在我已经在寻找很多答案了,我认为这必须是一个常见的陷阱" 掉进.

Now I have search a lot for an answer and I think this must be a common "trap" to fall into.

感谢您的帮助...

推荐答案

根据

According to the Rails Documentation for fields_for, you can also specify the record object after the record name.

所以这样的事情应该起作用...

So something like this should work...

<%= f.fields_for :profile_items, @profile_items do |f2| %>
  <%= render 'profile_item_fields', :f => f2 %>
<% end %>

这篇关于Rails 3 fields_for-排序顺序丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆