Rails 5 保存嵌套的问题 fields_for .. 错误消息 <xxx> 必须存在
Rails 5 trouble with saving a nested fields_for .. error message <xxx> must exist
我有以下型号:
class Person < ApplicationRecord
has_many :interests, dependent: :destroy
accepts_nested_attributes_for :interests
validates_presence_of :email
validates_inclusion_of :gender, :in => %w(M F), message: "Gender can only be in M or F"
has_secure_password
def name
"#{first_name} #{last_name}"
end
def interests_concatenated
interests.map { |i| i.interest }.join(", ")
end
end
class Interest < ApplicationRecord
belongs_to :person
end
我的控制器如下:
class PeopleController < ApplicationController
def index
@person = Person.all
end
def new
@person = Person.new
@person.interests.build
end
def create
@person = Person.new(people_params)
if @person.save
session[:user_id] = @person.id
redirect_to(people_path)
else
flash = "Email or gender can't be blank!"
render 'new'
end
end
private
def people_params
params.require(:person).permit(:email, :first_name, :last_name, :gender, :password,:password_confirmation, interests_attributes: [:hobby])
end
end
我的表格如下:
<%= form_for @person do |f| %>
<p>
<%= f.label :email %> <br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :first_name %> <br>
<%= f.text_field :first_name %>
</p>
<p>
<%= f.label :last_name %> <br>
<%= f.text_field :last_name %>
</p>
<p>
<%= f.label :gender %> <br>
<%= f.label(:gender_male, "Male") %>
<%= f.radio_button(:gender, "M") %> <br>
<%= f.label(:gender_female, "Female") %>
<%= f.radio_button(:gender, "F") %> <br>
</p>
<p>
<%= f.label :password %> <br>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %> <br>
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.fields_for :interests do |i| %>
<%= i.label :hobby %>
<%= i.text_field :hobby %>
<% end %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
这是我 运行 它时的 byebug 控制台日志:
非常困惑为什么它不起作用。会不会跟参数有关?
这是我提交表单时的日志文件:
而不是:
@interests = @person.interests.new
尝试
@interests = @person.interests.build
new
创建了一个全新的、干净的、完全空的新对象...但是 build
是特殊的 Rails 关联方法,它将用适当的默认值填充它(比如,例如右边person_id
)
我通过将此添加到我的兴趣模型中找到了一个可行的解决方案:
class Interest < ApplicationRecord
belongs_to :person, **optional: true**
end
由于@person 每次都保存失败,最大的线索在错误消息 "Interest person must exist" 中,我发现 to be helpful. Also this blog post on why this is needed 有助于阐明问题。
感谢参与其中的所有人!
我有以下型号:
class Person < ApplicationRecord
has_many :interests, dependent: :destroy
accepts_nested_attributes_for :interests
validates_presence_of :email
validates_inclusion_of :gender, :in => %w(M F), message: "Gender can only be in M or F"
has_secure_password
def name
"#{first_name} #{last_name}"
end
def interests_concatenated
interests.map { |i| i.interest }.join(", ")
end
end
class Interest < ApplicationRecord
belongs_to :person
end
我的控制器如下:
class PeopleController < ApplicationController
def index
@person = Person.all
end
def new
@person = Person.new
@person.interests.build
end
def create
@person = Person.new(people_params)
if @person.save
session[:user_id] = @person.id
redirect_to(people_path)
else
flash = "Email or gender can't be blank!"
render 'new'
end
end
private
def people_params
params.require(:person).permit(:email, :first_name, :last_name, :gender, :password,:password_confirmation, interests_attributes: [:hobby])
end
end
我的表格如下:
<%= form_for @person do |f| %>
<p>
<%= f.label :email %> <br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :first_name %> <br>
<%= f.text_field :first_name %>
</p>
<p>
<%= f.label :last_name %> <br>
<%= f.text_field :last_name %>
</p>
<p>
<%= f.label :gender %> <br>
<%= f.label(:gender_male, "Male") %>
<%= f.radio_button(:gender, "M") %> <br>
<%= f.label(:gender_female, "Female") %>
<%= f.radio_button(:gender, "F") %> <br>
</p>
<p>
<%= f.label :password %> <br>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %> <br>
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.fields_for :interests do |i| %>
<%= i.label :hobby %>
<%= i.text_field :hobby %>
<% end %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
这是我 运行 它时的 byebug 控制台日志:
非常困惑为什么它不起作用。会不会跟参数有关?
这是我提交表单时的日志文件:
而不是:
@interests = @person.interests.new
尝试
@interests = @person.interests.build
new
创建了一个全新的、干净的、完全空的新对象...但是 build
是特殊的 Rails 关联方法,它将用适当的默认值填充它(比如,例如右边person_id
)
我通过将此添加到我的兴趣模型中找到了一个可行的解决方案:
class Interest < ApplicationRecord
belongs_to :person, **optional: true**
end
由于@person 每次都保存失败,最大的线索在错误消息 "Interest person must exist" 中,我发现
感谢参与其中的所有人!