无法使具有 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
和这个视图:
<%= form_for :user,:url => users_path,:method => :post do |f| %>
<%= f.fields_for :city do |b| %>
<%= b.collection_select :id,City.all,:id,:name %>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我试图允许用户从已添加的城市列表中选择一个城市。我正在尝试向他展示一个选择。它的选择部分可以工作,但为其生成的 html 代码如下所示:
<select name="user[city][id]" id="user_city_id">
<option value="1">One</option>
<option value="2">Two</option>
</select>
请注意,它的名称在任何地方都没有 attribute
。因此,当我尝试保存它时,出现此错误:
City(#37815120) expected, got ActiveSupport::HashWithIndifferentAccess(#32969916)
如何解决此问题?
编辑:有一些进展,我尝试将 fields_for 更改为:
<%= f.fields_for :city_attributes do |b| %>
<%= b.collection_select :id,City.all,:id,:name %>
<% end %>
现在,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
我不知道这是否是正确的方法,但看起来不错。
I have these models:
class User < ActiveRecord::Base
has_one :city
accepts_nested_attributes_for :city
end
class City < ActiveRecord::Base
belongs_to :user
end
This controller action:
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
and this view:
<%= form_for :user,:url => users_path,:method => :post do |f| %>
<%= f.fields_for :city do |b| %>
<%= b.collection_select :id,City.all,:id,:name %>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I am trying to allow the user to select a city from the list of already added cities. I am trying to present him a select. The select part it works, but the generated html code for it, looks like this:
<select name="user[city][id]" id="user_city_id">
<option value="1">One</option>
<option value="2">Two</option>
</select>
Notice that it's name doesn't have attribute
anywhere. So, when I try to save it, I get this error:
City(#37815120) expected, got ActiveSupport::HashWithIndifferentAccess(#32969916)
How can I fix this?
EDIT: there is some progress, I tried to change the fields_for to this:
<%= f.fields_for :city_attributes do |b| %>
<%= b.collection_select :id,City.all,:id,:name %>
<% end %>
and now, the html seems to generate correctly. But I get this error now:
Couldn't find City with ID=1 for User with ID=
I have no idea what to do next.
EDIT2: overriding the city_attributes=
method seems to work:
def city_attributes=(attribs)
self.city = City.find(attribs[:id])
end
I don't know if it's the way to go, but it seems good.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看这个与你的问题相似的问题:
Rails 3:“accepts_nested_attributes_for”如何工作?
实际上,由于城市已经存在,我认为这里不需要嵌套形式。
尝试替换
为
更新后的评论
您可以更改与的关系(并相应地更新数据库方案)
然后尝试使用:
Have a look at this question that seems similar to yours :
Rails 3: How does "accepts_nested_attributes_for" work?
Actually, since the Cities already exsit, I think there is no need for nested forms here.
Try Replacing
With
Updated afters comments
Could you change your relationship with (and update database scheme accordingly)
And then try using:
您还可以在视图中执行
<%= f.collection_select :city_id, City.all, :id, :name %>
,然后将虚拟属性添加到您的 User 模型:
这可能不会非常干净,因为每当将 ID 分配给 some_user.city_id 时,都会“保存”关联的 City 模型。然而,这个解决方案使您的控制器和视图保持美观和干净。
注意:您可能还需要考虑传递给 setter 方法的空白 ID。
You could also do a
<%= f.collection_select :city_id, City.all, :id, :name %>
in your view and then add virtual attributes to your User model:
This might not be very clean, since the associated City model is "saved" whenever assigning an ID to some_user.city_id. However, this solution keeps your controller and view nice and clean.
Note: you might also want to account for a blank ID being passed in to the setter method.
试试这个
<%= f.select(:city_id, City.all.collect {|p| [ p.name, p.id ] }) %>
Try this
<%= f.select(:city_id, City.all.collect {|p| [ p.name, p.id ] }) %>