Ruby on rails RubyonRails |一个表单-三个表(不能保存表单内容)
我试图为用户的详细信息编写一个简单的应用程序表单,将数据保存到三个不同的表中(1.用户,2.公司,3.地址)。 我使用“属于”和“有很多”设置了一些关联,还使用嵌套的属性和字段来构建一个表单。但是我不能保存数据。 我是一个新手,所以可能我犯了一些愚蠢的错误,但我真的无法找到他们,因为3天。也尝试在谷歌和这里的论坛帖子中查找,但没有找到一个代码,其中的问题与child类有关。我在用户、公司和地址之间的联系很奇怪。有人能帮我吗 这是我的表格:Ruby on rails RubyonRails |一个表单-三个表(不能保存表单内容),ruby-on-rails,ruby,simple-form,nested-attributes,Ruby On Rails,Ruby,Simple Form,Nested Attributes,我试图为用户的详细信息编写一个简单的应用程序表单,将数据保存到三个不同的表中(1.用户,2.公司,3.地址)。 我使用“属于”和“有很多”设置了一些关联,还使用嵌套的属性和字段来构建一个表单。但是我不能保存数据。 我是一个新手,所以可能我犯了一些愚蠢的错误,但我真的无法找到他们,因为3天。也尝试在谷歌和这里的论坛帖子中查找,但没有找到一个代码,其中的问题与child类有关。我在用户、公司和地址之间的联系很奇怪。有人能帮我吗 这是我的表格: <%= simple_form_for @user
<%= simple_form_for @user do |f| %>
<h5>Personal Details</h5>
<%= f.input :firstname, label: 'Your First Name:' %>
<%= f.input :lastname, label: 'Your Last Name:' %>
<%= f.input :email, label: 'Your Email:' %>
<%= f.input :dateofbirth, label: 'Your Date of Birth:' %>
<%= f.input :phonenumber, label: 'Your Phone Number' %>
<h5>Adress</h5>
<%= f.fields_for :adresses do |g| %>
<%= g.input :street, label: 'Street:' %>
<%= g.input :city, label: 'City:' %>
<%= g.input :zipcode, label: 'Zip Code:' %>
<%= g.input :country, label: 'Country:' %>
<% end %>
<h5>Company</h5>
<%= f.fields_for :companies do |h| %>
<%= h.input :name, label: 'Name:' %>
<% end %>
<%= f.fields_for :adresses do |i| %>
<%= i.input :comstreet, label: 'Street:' %>
<%= i.input :comcity, label: 'City:' %>
<%= i.input :comzipcode, label: 'Zip Code:' %>
<%= i.input :comcountry, label: 'Country:' %>
<% end %>
<%= f.button :submit %>
<% end %>
<%= simple_form_for @user do |f| %>
<h5>Personal Details</h5>
<%= f.input :firstname, label: 'Your First Name:' %>
<%= f.input :lastname, label: 'Your Last Name:' %>
<%= f.input :email, label: 'Your Email:' %>
<%= f.input :dateofbirth, label: 'Your Date of Birth:' %>
<%= f.input :phonenumber, label: 'Your Phone Number' %>
<h5>Adress</h5>
<%= f.fields_for :adress_attributes do |g| %>
<%= g.input :street, label: 'Street:' %>
<%= g.input :city, label: 'City:' %>
<%= g.input :zipcode, label: 'Zip Code:' %>
<%= g.input :country, label: 'Country:' %>
<% end %>
<h5>Company</h5>
<%= f.fields_for :company_attributes do |h| %>
<%= h.input :name, label: 'Name:' %>
<%= h.fields_for :adress_attributes do |i| %>
<%= i.input :street, label: 'Street:' %>
<%= i.input :city, label: 'City:' %>
<%= i.input :zipcode, label: 'Zip Code:' %>
<%= i.input :country, label: 'Country:' %>
<% end %>
<% end %>
<%= f.button :submit %>
<% end %>
个人资料
地址
单位
user.rb
class User < ApplicationRecord
belongs_to :company, inverse_of: :users
belongs_to :adress, inverse_of: :users
validates_presence_of :firstname, :lastname, :email
validates_length_of :firstname, :maximum => 100
validates_length_of :lastname, :maximum => 100
end
class User < ApplicationRecord
belongs_to :company, inverse_of: :users
belongs_to :adress, inverse_of: :users
validates_presence_of :firstname, :lastname, :email
validates_length_of :firstname, :maximum => 100
validates_length_of :lastname, :maximum => 100
accepts_nested_attributes_for :company
accepts_nested_attributes_for :address
end
class用户100的长度
验证的长度为:lastname,:max=>100
结束
company.rb
class Company < ApplicationRecord
has_many :users, inverse_of: :companies
belongs_to :adress, inverse_of: :companies
accepts_nested_attributes_for :users
end
class Company < ApplicationRecord
has_many :users, inverse_of: :companies
belongs_to :adress, inverse_of: :companies
accepts_nested_attributes_for :users
accepts_nested_attributes_for :adress
end
class公司
地址:rb
class Adress < ApplicationRecord
has_many :users, inverse_of: :adresses
has_many :companies, inverse_of: :adresses
accepts_nested_attributes_for :users, :companies
end
class Adress < ApplicationRecord
has_many :users, inverse_of: :adresses
has_many :companies, inverse_of: :adresses
accepts_nested_attributes_for :users, :companies
end
类地址
用户/控制器
class UsersController < ApplicationController
def index
end
def new
@user = User.new
@company = Company.new
@adress = Adress.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_path
end
end
private
def user_params
params.require(:user).permit(:firstname, :lastname, :email, :dateofbirth,
:phonenumber, company_attributes: [:name],
adress_attributes: [:street,:city, :zipcode, :country, :comstreet,
:comctiy, :comzipcode, :comcountry] )
end
end
class UsersController < ApplicationController
def index
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_path
end
end
private
def user_params
params.require(:user).permit(:firstname, :lastname, :email, :dateofbirth,
:phonenumber, company_attributes: [:name, adress_attributes: [:street,
:ctiy, :zipcode, :country]],
adress_attributes: [:street,:city, :zipcode, :country] )
end
end
class UsersController
在您的user.rb文件中:
class User < ApplicationRecord
belongs_to :company, inverse_of: :users
belongs_to :adress, inverse_of: :users
validates_presence_of :firstname, :lastname, :email
validates_length_of :firstname, :maximum => 100
validates_length_of :lastname, :maximum => 100
# add nested attributes here not in other models
accepts_nested_attributes_for :companies
accepts_nested_attributes_for :addresses
end
在视图中,删除address的多个表单元素。您写了两次。我对代码做了一些更改,并且能够保存表单,请检查 这是我的表格:
<%= simple_form_for @user do |f| %>
<h5>Personal Details</h5>
<%= f.input :firstname, label: 'Your First Name:' %>
<%= f.input :lastname, label: 'Your Last Name:' %>
<%= f.input :email, label: 'Your Email:' %>
<%= f.input :dateofbirth, label: 'Your Date of Birth:' %>
<%= f.input :phonenumber, label: 'Your Phone Number' %>
<h5>Adress</h5>
<%= f.fields_for :adresses do |g| %>
<%= g.input :street, label: 'Street:' %>
<%= g.input :city, label: 'City:' %>
<%= g.input :zipcode, label: 'Zip Code:' %>
<%= g.input :country, label: 'Country:' %>
<% end %>
<h5>Company</h5>
<%= f.fields_for :companies do |h| %>
<%= h.input :name, label: 'Name:' %>
<% end %>
<%= f.fields_for :adresses do |i| %>
<%= i.input :comstreet, label: 'Street:' %>
<%= i.input :comcity, label: 'City:' %>
<%= i.input :comzipcode, label: 'Zip Code:' %>
<%= i.input :comcountry, label: 'Country:' %>
<% end %>
<%= f.button :submit %>
<% end %>
<%= simple_form_for @user do |f| %>
<h5>Personal Details</h5>
<%= f.input :firstname, label: 'Your First Name:' %>
<%= f.input :lastname, label: 'Your Last Name:' %>
<%= f.input :email, label: 'Your Email:' %>
<%= f.input :dateofbirth, label: 'Your Date of Birth:' %>
<%= f.input :phonenumber, label: 'Your Phone Number' %>
<h5>Adress</h5>
<%= f.fields_for :adress_attributes do |g| %>
<%= g.input :street, label: 'Street:' %>
<%= g.input :city, label: 'City:' %>
<%= g.input :zipcode, label: 'Zip Code:' %>
<%= g.input :country, label: 'Country:' %>
<% end %>
<h5>Company</h5>
<%= f.fields_for :company_attributes do |h| %>
<%= h.input :name, label: 'Name:' %>
<%= h.fields_for :adress_attributes do |i| %>
<%= i.input :street, label: 'Street:' %>
<%= i.input :city, label: 'City:' %>
<%= i.input :zipcode, label: 'Zip Code:' %>
<%= i.input :country, label: 'Country:' %>
<% end %>
<% end %>
<%= f.button :submit %>
<% end %>
个人资料
地址
单位
user.rb
class User < ApplicationRecord
belongs_to :company, inverse_of: :users
belongs_to :adress, inverse_of: :users
validates_presence_of :firstname, :lastname, :email
validates_length_of :firstname, :maximum => 100
validates_length_of :lastname, :maximum => 100
end
class User < ApplicationRecord
belongs_to :company, inverse_of: :users
belongs_to :adress, inverse_of: :users
validates_presence_of :firstname, :lastname, :email
validates_length_of :firstname, :maximum => 100
validates_length_of :lastname, :maximum => 100
accepts_nested_attributes_for :company
accepts_nested_attributes_for :address
end
class用户100的长度
验证的长度为:lastname,:max=>100
接受公司的\u嵌套\u属性\u
接受地址的\u嵌套\u属性\u
结束
company.rb
class Company < ApplicationRecord
has_many :users, inverse_of: :companies
belongs_to :adress, inverse_of: :companies
accepts_nested_attributes_for :users
end
class Company < ApplicationRecord
has_many :users, inverse_of: :companies
belongs_to :adress, inverse_of: :companies
accepts_nested_attributes_for :users
accepts_nested_attributes_for :adress
end
class公司
地址:rb
class Adress < ApplicationRecord
has_many :users, inverse_of: :adresses
has_many :companies, inverse_of: :adresses
accepts_nested_attributes_for :users, :companies
end
class Adress < ApplicationRecord
has_many :users, inverse_of: :adresses
has_many :companies, inverse_of: :adresses
accepts_nested_attributes_for :users, :companies
end
类地址
用户/控制器
class UsersController < ApplicationController
def index
end
def new
@user = User.new
@company = Company.new
@adress = Adress.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_path
end
end
private
def user_params
params.require(:user).permit(:firstname, :lastname, :email, :dateofbirth,
:phonenumber, company_attributes: [:name],
adress_attributes: [:street,:city, :zipcode, :country, :comstreet,
:comctiy, :comzipcode, :comcountry] )
end
end
class UsersController < ApplicationController
def index
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_path
end
end
private
def user_params
params.require(:user).permit(:firstname, :lastname, :email, :dateofbirth,
:phonenumber, company_attributes: [:name, adress_attributes: [:street,
:ctiy, :zipcode, :country]],
adress_attributes: [:street,:city, :zipcode, :country] )
end
end
class UsersController
它现在正在工作,能够用公司地址保存用户、地址和公司。
您是否发现任何错误?还要检查用户参数中的拼写…:comcity不正确。我甚至无法保存,因此无法读取错误列表。但这里有一个屏幕,在我点击提交后,我的控制台会发生什么:谢谢你的建议!实际上,我仍然无法保存它:(这是控制台的错误:@鸽子场您缺少公司的地址\属性,请尝试使用保存检查错误!它将引发异常,以便您能够找到确切的错误。您必须在公司\属性部分使用h.adress\属性。