[英]Nested form in Ruby on Rails 4.2, cant recognize the params from fields_for
undefined method `hname' for #<Wife:0x007f1eb7f91230> Did you mean? wname
Hi! 嗨! I'm new to ruby and am learning how to make a nested form manually and not using a scaffold for about 1 week now and still couldnt find a way to make it, i've done lots of research but still couldn't find the right solution.
我是红宝石的新手,正在学习如何手动制作嵌套表格,并且大约一星期不使用脚手架,但仍然找不到找到它的方法,我做了很多研究,但仍然找不到正确的解决方案。 Please help me know what are my problems and missing syntax.
请帮助我知道我的问题和缺少的语法。 Thanks in advance!
提前致谢!
here is my tables: 这是我的桌子:
mysql> desc husbands;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| hname | varchar(255) | YES | | NULL | |
| wife_id | int(11) | YES | MUL | NULL | |
| kabet_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
mysql> desc wives;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| wname | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> desc kabets;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| kname | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
Here are my codes: 这是我的代码:
wife.rb 老婆
class Wife < ActiveRecord::Base
has_one :husband
accepts_nested_attributes_for :husband
validates_presence_of :hname
end
husband.rb 丈夫
class Husband < ActiveRecord::Base
belongs_to :wife
belongs_to :kabet
accepts_nested_attributes_for :wife
end
kabet.rb kabet.rb
class Kabet < ActiveRecord::Base
has_one :husband
accepts_nested_attributes_for :husband
end
husbands controller: 丈夫控制人:
class HusbandsController < ApplicationController
def index
@husbands = Husband.all
end
def show
@husband = Husband.find(params[:id])
end
def new
@husband = Husband.new
@husband.build_wife
@husband.build_kabet
end
def create
@husband = Husband.new(husband_params)
@husband.build_wife
@husband.build_kabet
@husband.save
redirect_to action: "show"
end
private
def husband_params
params.require(:husband).permit(:hname, :wife_id, :kabet_id, wife_attributes:[:id,:wname], kabets_attributes:[:id,:kname])
end
end
wives controller: 妻子控制器:
class WivesController < ApplicationController
def index
@wives = Wife.all
end
def new
@wife = Wife.new
@wife.build_husband
end
def create
@wife = Wife.new(wife_params)
#@wife.build_husband
@wife.save
redirect_to action: "index"
end
def show
end
private
def wife_params
params.require(:wife).permit(:id,:wname, husband_attributes: [:id,:hname])
end
end
view form for husband: 查看丈夫的表格:
<%= form_for @husband do |f| %>
<table>
<tr>
<th>Name ng Husband</th>
<th>Name ng Legal Wife</th>
<th>Name ng Kabet</th>
</tr>
<tr>
<td><%= f.text_field :hname %></td>
<%= f.fields_for :wife do |wife| %>
<td><%= wife.text_field :wname %></td>
<% end %>
<%= f.fields_for :kabets do |kabet| %>
<td><%= kabet.text_field :kname %></td>
<% end %>
</tr>
<%= f.submit %>
</table>
<% end %>
view form for wives: 查看妻子的表格:
<%= form_for @wife do |f| %>
<table>
<tr>
<th><%= f.label :wname %></th>
<td><%= f.text_field :wname %></td>
</tr>
<%= f.fields_for :husbands do |ff| %>
<tr>
<th><%= ff.label :hname %></th>
<td><%= ff.text_field :hname %></td>
<% end %>
</tr>
</table>
<%= f.submit %>
<% end %>
It doesnt matter even if i create on husband's form or on wives' form, i still encounter this error: 即使我以丈夫的形式或妻子的形式进行创建也没关系,我仍然会遇到此错误:
undefined method `hname' for #<Wife id: nil, wname: "zzxc", created_at: nil, updated_at: nil> Did you mean? wname
Extracted source (around line #14):
In your Wife
model, you have mentioned validates_presence_of :hname
but you dont have a hname
in Wife
table. 在您的
Wife
模型中,您提到了validates_presence_of :hname
但是在Wife
表中没有hname
。 It is in husband
table. 在
husband
桌子上。 That is why it is giving that error. 这就是为什么它会给出该错误。
Instead, you can move that validation to Husband
model and in Wife
model use: 相反,您可以将该验证移至“
Husband
模型并在“ Wife
模型中使用:
validates_associated: husband
if you really want to validate hanme
name before saving wife
object. validates_associated: husband
如果您真的想在保存wife
对象之前验证hanme
名称。
Please note that , I have not checked your codes for any other possible mistakes in nested form
setup. 请注意,在
nested form
设置中,我尚未检查您的代码是否存在任何其他可能的错误。 Fix this error first and then you can proceed further, step by step. 首先修复此错误,然后可以逐步进行进一步操作。
In your WivesController
why do you have #@wife.build_husband
commented. 在您的
WivesController
为什么要注释#@wife.build_husband
。 That alone is one source of your problems. 仅此一项就可以解决您的问题。 Uncomment that line and everything should be fine.
取消注释该行,一切都应该没问题。 If it doesn't work try the following
如果不起作用,请尝试以下操作
Use pry
in your wives view
and try to call its association on it like this @wives.husband
if you get nil, then you will have to build the association like this @wives.build_husband
. 使用
pry
你的wives view
并尝试调用它的关联上像这样@wives.husband
如果你得到零,那么你将不得不建立这样的协会@wives.build_husband
。 I know you have already built this in the controller, but for debugging sake, try it building in on the view and see how things go. 我知道您已经在控制器中构建了它,但是为了调试起见,请尝试在视图中构建它,然后看看情况如何。
validates_presence_of :hname should be done in the Husband
model not in the Wife
model. validates_presence_of:hname应该在
Husband
模型中而不是在Wife
模型中完成。 Alternatively you can validate the presence of hname
for nested attributes like this 另外,您可以像这样验证嵌套属性的
hname
是否存在
accepts_nested_attributes_for :husband, reject_if: proc { |attributes| attributes['hname'].blank? }
Hope this helps you out. 希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.