[英]Rails form object doesn't update database with polymorphic association
I've searched and searched and nothing came up, so I need to ask you for help. 我搜索了一下,没有发现任何问题,因此我需要向您寻求帮助。 I have to create registration form with nesting fields with separate tables in database for user, company and address.
我必须使用用户,公司和地址的数据库中的单独表创建带有嵌套字段的注册表单。 Fields structure below (column names in db as follow):
字段结构如下(db中的列名如下):
User(new model)
-first name
-email
Address(nested in User)
-street
-city
Company(nested in User)
- name
- Address(nested in user.company)
-street
-city
The thing is that when I'm trying to save submit forms to the database I've got an error unknown attribute 'company_name' for Company
. 事实是,当我尝试将提交表单保存到数据库时,出现
unknown attribute 'company_name' for Company
的错误unknown attribute 'company_name' for Company
。 It shows me that my model has name
attribute instead of company_name
and it's true but how to associate them for company_name which is reflected to the :name column in company table? 它向我展示了我的模型具有
name
属性而不是company_name
,这是事实,但是如何将它们与company_name关联起来,这反映到company表中的:name列? I think I should map my params but how to do so? 我想我应该映射我的参数,但是该怎么做呢?
I associated them with polymorphic associations, my models are: 我将它们与多态关联相关联,我的模型是:
class User < ApplicationRecord
has_one :address, as: :addressable
has_one :company
accepts_nested_attributes_for :address
end
class Address < ApplicationRecord
belongs_to :addressable, polymorphic: true
end
class Company < ApplicationRecord
belongs_to :user
has_one :address, as: :addressable
accepts_nested_attributes_for :address
end
My registrations controller 我的注册管理员
def create
@registration = Registration.new(registration_params)
end
private
def registration_params
params.require(:registration).permit(
:first_name, :email,
:street, :city,
:company_street, :company_city
)
end
forms/registration.rb 表格/ registration.rb
class Registration
include ActiveModel::Model
attr_accessor :first_name, :email,
:street, :city,
:company_street, :company_city
attr_reader :params
def address_params
params.slice(:street, :city, :zip_code, :country)
end
def create_company
company.save(company_params)
end
def create_company_address
company.create_address(company_address_params)
end
private
def company_params
params.slice(:company_name)
end
def company_address_params
params.slice(:company_street, :company_city)
end
EDIT: missing view: 编辑:缺少视图:
<%= simple_form_for(@registration, url: registration_path(resource_name)) do |f| %>
<%= f.input :email, autofocus: true, autocomplete: "email", class: "form-control" %>
<%= f.input :first_name, class: "form-control" %>
<%= f.input :street %>
<%= f.input :city %>
<%= f.input :zip_code %>
<hr>
<h3>Company details</h3>
<%= f.input :company_name %>
<%= f.input :company_street %>
<%= f.input :company_city %>
<% end %>
I'd recommend you to read about how accepts_nested_attributes_for
works and form's helper fields_for
. 我建议您阅读
accepts_nested_attributes_for
工作方式以及表单帮助器fields_for
。
For the company with an addres, for example, you have to use accepts_nested_attributes_for
for the Company too on the User model and the form should be something like: 例如,对于具有地址的公司,您也必须在用户模型上对公司也使用
accepts_nested_attributes_for
,并且表单应类似于:
<h3>Company details</h3>
<%= f.simple_fields_for :company, Company.new do |ff| %>
<%= ff.input :name %>
<%= ff.simple_fields_for :address, Address.new do |fff| %>
<%= fff.input :street %>
<%= fff.input :city %>
<% end %>
<% end %>
Then the parameters: 然后参数:
def user_params
params.require(:user).permit(:email, company_attributes: [:name, address_attributes: [:street, :city])
end
I really recommend you to read about nested forms, fields_for
method and the accepts_nested_attributes_for
config on the models (and also about StrongParameters), it will make much more sense to my answer. 我真的建议您阅读有关模型上的嵌套表单,
fields_for
方法和accepts_nested_attributes_for
配置(以及有关StrongParameters)的信息,这对我的答案将更为有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.