无法使用has_one关联获得嵌套表单

我有这些模型:

class User < ActiveRecord::Base has_one :city accepts_nested_attributes_for :city end class City < ActiveRecord::Base belongs_to :user end 

这个控制器动作:

  def create @user = User.new(params[:user]) respond_to do |format| if @user.save format.html { redirect_to(@user, :notice => 'User was successfully created.') } format.xml { render :xml => @user, :status => :created, :location => @user } else format.html { render :action => "new" } format.xml { render :xml => @user.errors, :status => :unprocessable_entity } end end end 

而这个观点:

  users_path,:method => :post do |f| %>    

我试图允许用户从已添加的城市列表中选择一个城市。 我想给他一个选择。 它工作的选择部分,但生成的html代码,如下所示:

  One Two  

请注意,它的名称在任何地方都没有attribute 。 所以,当我尝试保存它时,我收到此错误:

 City(#37815120) expected, got ActiveSupport::HashWithIndifferentAccess(#32969916) 

我怎样才能解决这个问题?

编辑:有一些进展,我试图将fields_for更改为:

    

而现在,html似乎正确生成。 但我现在得到这个错误:

 Couldn't find City with ID=1 for User with ID= 

我不知道接下来该做什么。

EDIT2:覆盖city_attributes=方法似乎工作:

 def city_attributes=(attribs) self.city = City.find(attribs[:id]) end 

我不知道这是否可行,但似乎很好。

看看这个与你的问题类似的问题: Rails 3:“accepts_nested_attributes_for”如何工作?

实际上,由于城市已经存在,我认为这里不需要嵌套表格。

尝试更换

 <%= f.fields_for :city_attributes do |b| %> <%= b.collection_select :id,City.all,:id,:name %> <% end %> 

 <%= f.collection_select :city, City.all,:id,:name %> 

更新后的评论

你可以改变你的关系(并相应地更新数据库方案)

 class User < ActiveRecord::Base belongs_to :city end class City < ActiveRecord::Base has_many :users end 

然后尝试使用:

 <%= f.collection_select :city_id, City.all,:id,:name %> 

你也可以做一个

<%= f.collection_select :city_id, City.all, :id, :name %>

在您的视图中,然后将虚拟属性添加到您的用户模型:

 class User < ActiveRecord::Base ... def city_id(c_id) update_attribute(:city, City.find(c_id)) end def city_id city.id end end 

这可能不是很干净,因为只要为some_user.city_id分配ID,就会“保存”关联的City模型。 但是,此解决方案可使您的控制器和视图保持良好和清洁。

注意:您可能还想考虑传入setter方法的空白ID。

试试这个

<%= f.select(:city_id,City.all.collect {| p | [p.name,p.id]})%>