不允许的参数 rails 5.1.1
Unpermited params rails 5.1.1
Rails 5.1.1 Ruby 2.4.1
创建新组织时,联系人信息应保存到联系人table,但事实并非如此。我对 rails 还是比较陌生,并且浏览了其他具有强大参数的帖子但没有运气。我想我已经包括了所有必要的部分来让这个工作如果没有让我知道并谢谢你!
创建操作中 logger.info 的控制台输出
13:55:03 rails.1 | User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
13:55:03 rails.1 | Role Load (0.5ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
13:55:03 rails.1 | <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"LIdSKBh7x9Dqs1A6gKb0Gn7EecArG6aflOeC4OARShLwfySH+HQ5joN3FUCe6qmJBGn2K/QRize67qhrxczK+w==", "organization"=><ActionController::Parameters {"name"=>"Apple", "label"=>"apl", "proxy_hostname"=>"www.apple.com"} permitted: false>, "contact_attributes"=>{"name"=>"555-555-5555"}, "commit"=>"Create Organization", "controller"=>"organizations", "action"=>"create"} permitted: false>
13:55:03 rails.1 | <ActionController::Parameters {"name"=>"Apple", "label"=>"apl", "proxy_hostname"=>"www.apple.com"} permitted: true>
13:55:03 rails.1 | (0.2ms) BEGIN
13:55:03 rails.1 | Organization Exists (0.4ms) SELECT 1 AS one FROM `organizations` WHERE `organizations`.`label` = BINARY 'apl' LIMIT 1
13:55:03 rails.1 | SQL (0.4ms) INSERT INTO `organizations` (`name`, `label`, `created_at`, `updated_at`, `proxy_hostname`) VALUES ('Apple', 'apl', '2017-06-20 17:55:03', '2017-06-20 17:55:03', 'www.apple.com')
13:55:03 rails.1 | (0.5ms) COMMIT
13:55:03 rails.1 | method=POST path=/organizations format=html controller=OrganizationsController action=create status=302 duration=10.23 view=0.00 db=2.12 location=http://localhost:5000/organizations/apl
13:55:03 rails.1 | User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
13:55:03 rails.1 | Organization Load (0.2ms) SELECT `organizations`.* FROM `organizations` WHERE `organizations`.`label` = 'apl' LIMIT 1
13:55:03 rails.1 | (0.5ms) SELECT COUNT(*) FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'analyst') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
13:55:03 rails.1 | User Load (0.3ms) SELECT `users`.* FROM `users` INNER JOIN `organization_users` ON `users`.`id` = `organization_users`.`user_id` WHERE `organization_users`.`organization_id` = 25
13:55:03 rails.1 | Sensor Load (0.2ms) SELECT `sensors`.* FROM `sensors` WHERE `sensors`.`organization_id` = '25'
13:55:03 rails.1 | CACHE (0.0ms) SELECT COUNT(*) FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'analyst') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
13:55:03 rails.1 | Role Load (0.5ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
13:55:03 rails.1 | CACHE Role Load (0.0ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
型号
class Organization < ApplicationRecord
belongs_to :contact, optional: true
accepts_nested_attributes_for :contact
end
class Contact < ApplicationRecord
has_one :organization
end
控制器
class OrganizationsController < ApplicationController
before_action :set_organization, only: [:show, :edit, :update,
:destroy]
after_action :verify_authorized, except: :index
after_action :verify_policy_scoped, only: :index
def index
@organizations = policy_scope(Organization)
end
def show
authorize @organization
end
def new
@organization = Organization.new(contact: Contact.new)
authorize @organization, :create?
end
def edit
authorize @organization, :update?
end
def create
@organization = Organization.new(organization_params)
authorize @organization
logger.info(params.inspect)
logger.info(organization_params.inspect)
respond_to do |format|
if @organization.save
format.html { redirect_to @organization, notice: 'Organization
was
successfully created.' }
format.json { render :show, status: :created, location:
@organization }
else
format.html { render :new }
format.json { render json: @organization.errors, status:
:unprocessable_entity }
end
end
end
def update
authorize @organization
respond_to do |format|
if @organization.update(organization_params)
format.html { redirect_to @organization, notice: 'Organization
was
successfully updated.' }
format.json { render :show, status: :ok, location:
@organization }
else
format.html { render :edit }
format.json { render json: @organization.errors, status:
:unprocessable_entity }
end
end
end
def destroy
authorize @organization
@organization.destroy
respond_to do |format|
format.html { redirect_to organizations_url, notice:
'Organization
was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_organization
@organization = Organization.find_by_label(params[:id])
end
def organization_params
params.require(:organization).permit(:name, :label,
:proxy_hostname,
:contact_id, contact_attributes: [:name, :email, :phone])
end
end
表格
= form_for @organization do |f|
- if @organization.errors.any?
#error_explanation
h2 = "#{pluralize(@organization.errors.count, "error")} prohibited
this organization from being saved:"
ul
- @organization.errors.full_messages.each do |message|
li = message
.field.form-group
= f.label :name, for: :organization_name
= f.text_field :name, class: 'form-control', autofocus: true
.field.form-group
= f.label :label, for: :organization_label
= f.text_field :label, class: 'form-control'
.field.form-group
= f.label :proxy_hostname, for: :organization_proxy_hostname
= f.text_field :proxy_hostname, class: 'form-control'
= fields_for :contact_attributes do |ff|
.field.form-group
= ff.label :Contact_Name, for: :contact_name
= ff.text_field :name, class: 'form-control'
= ff.label :Email, for: :contact_email
= ff.text_field :name, class: 'form-control'
= ff.label :Contact_Phone, for: :contact_phone
= ff.text_field :name, class: 'form-control'
.actions = f.submit class: 'btn btn-outline-primary'
您的 contact_attributes 未嵌套在组织内部,但在您的 organization_params 中您希望 contact_attributes 嵌套。修复您的观点,因此 contact_attributes 嵌套在组织中。
在 contract_attributes
的视图中进行以下更改
f.fields_for :contract do |ff|
如果您查看参数的形状,您会发现 organization
和 contact_attributes
是兄弟姐妹,而不是父级嵌套。这与你的fields_for
有关。在我的脑海中,你必须这样做:
= fields_for 'organization[contact_attributes]' do |ff|
或以下形式:
= fields_for organization.contact do |ff|
尽管在这种情况下,您显然必须确保它不是零。
或者查看@Ramon 的回答,我认为他做到了。
在
的字段前添加 f.
= f.fields_for :contact_attributes do |ff|
.field.form-group
= ff.label :Contact_Name, for: :contact_name
= ff.text_field :name, class: 'form-control'
= ff.label :Email, for: :contact_email
= ff.text_field :name, class: 'form-control'
= ff.label :Contact_Phone, for: :contact_phone
= ff.text_field :name, class: 'form-control'
.actions = f.submit class: 'btn btn-outline-primary'
Rails 5.1.1 Ruby 2.4.1
创建新组织时,联系人信息应保存到联系人table,但事实并非如此。我对 rails 还是比较陌生,并且浏览了其他具有强大参数的帖子但没有运气。我想我已经包括了所有必要的部分来让这个工作如果没有让我知道并谢谢你!
创建操作中 logger.info 的控制台输出
13:55:03 rails.1 | User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
13:55:03 rails.1 | Role Load (0.5ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
13:55:03 rails.1 | <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"LIdSKBh7x9Dqs1A6gKb0Gn7EecArG6aflOeC4OARShLwfySH+HQ5joN3FUCe6qmJBGn2K/QRize67qhrxczK+w==", "organization"=><ActionController::Parameters {"name"=>"Apple", "label"=>"apl", "proxy_hostname"=>"www.apple.com"} permitted: false>, "contact_attributes"=>{"name"=>"555-555-5555"}, "commit"=>"Create Organization", "controller"=>"organizations", "action"=>"create"} permitted: false>
13:55:03 rails.1 | <ActionController::Parameters {"name"=>"Apple", "label"=>"apl", "proxy_hostname"=>"www.apple.com"} permitted: true>
13:55:03 rails.1 | (0.2ms) BEGIN
13:55:03 rails.1 | Organization Exists (0.4ms) SELECT 1 AS one FROM `organizations` WHERE `organizations`.`label` = BINARY 'apl' LIMIT 1
13:55:03 rails.1 | SQL (0.4ms) INSERT INTO `organizations` (`name`, `label`, `created_at`, `updated_at`, `proxy_hostname`) VALUES ('Apple', 'apl', '2017-06-20 17:55:03', '2017-06-20 17:55:03', 'www.apple.com')
13:55:03 rails.1 | (0.5ms) COMMIT
13:55:03 rails.1 | method=POST path=/organizations format=html controller=OrganizationsController action=create status=302 duration=10.23 view=0.00 db=2.12 location=http://localhost:5000/organizations/apl
13:55:03 rails.1 | User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
13:55:03 rails.1 | Organization Load (0.2ms) SELECT `organizations`.* FROM `organizations` WHERE `organizations`.`label` = 'apl' LIMIT 1
13:55:03 rails.1 | (0.5ms) SELECT COUNT(*) FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'analyst') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
13:55:03 rails.1 | User Load (0.3ms) SELECT `users`.* FROM `users` INNER JOIN `organization_users` ON `users`.`id` = `organization_users`.`user_id` WHERE `organization_users`.`organization_id` = 25
13:55:03 rails.1 | Sensor Load (0.2ms) SELECT `sensors`.* FROM `sensors` WHERE `sensors`.`organization_id` = '25'
13:55:03 rails.1 | CACHE (0.0ms) SELECT COUNT(*) FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'analyst') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
13:55:03 rails.1 | Role Load (0.5ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
13:55:03 rails.1 | CACHE Role Load (0.0ms) SELECT `roles`.* FROM `roles` INNER JOIN `users_roles` ON `roles`.`id` = `users_roles`.`role_id` WHERE `users_roles`.`user_id` = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
型号
class Organization < ApplicationRecord
belongs_to :contact, optional: true
accepts_nested_attributes_for :contact
end
class Contact < ApplicationRecord
has_one :organization
end
控制器
class OrganizationsController < ApplicationController
before_action :set_organization, only: [:show, :edit, :update,
:destroy]
after_action :verify_authorized, except: :index
after_action :verify_policy_scoped, only: :index
def index
@organizations = policy_scope(Organization)
end
def show
authorize @organization
end
def new
@organization = Organization.new(contact: Contact.new)
authorize @organization, :create?
end
def edit
authorize @organization, :update?
end
def create
@organization = Organization.new(organization_params)
authorize @organization
logger.info(params.inspect)
logger.info(organization_params.inspect)
respond_to do |format|
if @organization.save
format.html { redirect_to @organization, notice: 'Organization
was
successfully created.' }
format.json { render :show, status: :created, location:
@organization }
else
format.html { render :new }
format.json { render json: @organization.errors, status:
:unprocessable_entity }
end
end
end
def update
authorize @organization
respond_to do |format|
if @organization.update(organization_params)
format.html { redirect_to @organization, notice: 'Organization
was
successfully updated.' }
format.json { render :show, status: :ok, location:
@organization }
else
format.html { render :edit }
format.json { render json: @organization.errors, status:
:unprocessable_entity }
end
end
end
def destroy
authorize @organization
@organization.destroy
respond_to do |format|
format.html { redirect_to organizations_url, notice:
'Organization
was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_organization
@organization = Organization.find_by_label(params[:id])
end
def organization_params
params.require(:organization).permit(:name, :label,
:proxy_hostname,
:contact_id, contact_attributes: [:name, :email, :phone])
end
end
表格
= form_for @organization do |f|
- if @organization.errors.any?
#error_explanation
h2 = "#{pluralize(@organization.errors.count, "error")} prohibited
this organization from being saved:"
ul
- @organization.errors.full_messages.each do |message|
li = message
.field.form-group
= f.label :name, for: :organization_name
= f.text_field :name, class: 'form-control', autofocus: true
.field.form-group
= f.label :label, for: :organization_label
= f.text_field :label, class: 'form-control'
.field.form-group
= f.label :proxy_hostname, for: :organization_proxy_hostname
= f.text_field :proxy_hostname, class: 'form-control'
= fields_for :contact_attributes do |ff|
.field.form-group
= ff.label :Contact_Name, for: :contact_name
= ff.text_field :name, class: 'form-control'
= ff.label :Email, for: :contact_email
= ff.text_field :name, class: 'form-control'
= ff.label :Contact_Phone, for: :contact_phone
= ff.text_field :name, class: 'form-control'
.actions = f.submit class: 'btn btn-outline-primary'
您的 contact_attributes 未嵌套在组织内部,但在您的 organization_params 中您希望 contact_attributes 嵌套。修复您的观点,因此 contact_attributes 嵌套在组织中。
在 contract_attributes
的视图中进行以下更改 f.fields_for :contract do |ff|
如果您查看参数的形状,您会发现 organization
和 contact_attributes
是兄弟姐妹,而不是父级嵌套。这与你的fields_for
有关。在我的脑海中,你必须这样做:
= fields_for 'organization[contact_attributes]' do |ff|
或以下形式:
= fields_for organization.contact do |ff|
尽管在这种情况下,您显然必须确保它不是零。
或者查看@Ramon 的回答,我认为他做到了。
在
的字段前添加f.
= f.fields_for :contact_attributes do |ff|
.field.form-group
= ff.label :Contact_Name, for: :contact_name
= ff.text_field :name, class: 'form-control'
= ff.label :Email, for: :contact_email
= ff.text_field :name, class: 'form-control'
= ff.label :Contact_Phone, for: :contact_phone
= ff.text_field :name, class: 'form-control'
.actions = f.submit class: 'btn btn-outline-primary'