undefined method `hname' for #<Wife:0x007f1eb7f91230> Did you mean? wname
嗨!我是ruby的新手,我正在学习如何手动制作嵌套表格而不是使用脚手架大约1周,但仍然无法找到制作它的方法,我做了很多研究,但仍然找不到正确的解决请帮助我知道我的问题和缺少语法。提前谢谢!
这是我的表格:
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)
以下是我的代码:
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
class Kabet < ActiveRecord::Base
has_one :husband
accepts_nested_attributes_for :husband
end
丈夫控制员:
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
妻子控制员:
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
丈夫的查看表格:
<%= 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 %>
查看妻子的表格:
<%= 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 %>
即使我在丈夫的表格或妻子的形式上创作也无所谓,我仍然会遇到这个错误:
undefined method `hname' for #<Wife id: nil, wname: "zzxc", created_at: nil, updated_at: nil> Did you mean? wname
Extracted source (around line #14):
答案 0 :(得分:0)
在Wife
模型中,您提到了validates_presence_of :hname
,但hname
表中没有Wife
。它在husband
表中。这就是它给出错误的原因。
相反,您可以将该验证移至Husband
模型并在Wife
模型中使用:
validates_associated: husband
如果您确实要在保存hanme
对象之前验证wife
名称。
请注意,我没有在nested form
设置中检查您的代码是否存在任何其他可能的错误。首先修复此错误,然后您可以继续进行,一步一步。
答案 1 :(得分:0)
在您的Cust no. Chg Key Value Size Name
61 A Ctot1 2 2 XA
61 A Ctot2 3 2 XA
61 A Ctot3 4 2 XA
61 A Ctot4 5 2 XA
中,您为什么WivesController
发表了评论。仅这一点就是你问题的一个原因。取消注释该行,一切都应该没问题。如果不起作用,请尝试以下
在#@wife.build_husband
中使用pry
并尝试在此wives view
上调用其关联,如果您获得nil,那么您将必须构建此类关联{ {1}}。我知道你已经在控制器中构建了它,但是为了调试,请尝试在视图中构建并查看事情的进展情况。
validates_presence_of:hname应在不在@wives.husband
模型中的@wives.build_husband
模型中完成。或者,您可以验证Husband
是否存在嵌套属性,例如
Wife
希望这可以帮助你。