0

Rails 5.1.1 Ruby 2.4.1Unpermited params rails 5.1.1

创建新组织时,联系人信息应保存到联系人表中,但情况并非如此。我还是比较新的铁杆,并通过其他职位与强大的参数看,没有运气。我想我已经包括了所有必要的部分来让这个工作,如果不让我知道和谢谢你!

从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' 
+0

的,什么是错误,到底是什么? –

+0

将参数片段重新格式化为多行。这是不可读的。 –

+0

@SergioTulentsev根据logger.info,它允许名称,标签和代理,但不允许contact_attributes。或者至少我是这么理解的? – paulkiio

回答

3

把你的领域之前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' 
+1

啊,很好!没有注意到。 –

+0

Doh!非常感谢! – paulkiio

1

你contact_attributes不是嵌套的组织内,但在你的organization_params你期待contact_attributes被嵌套。修复您的视图,因此contact_attributes嵌套在组织中。

使该视图中的以下更改contract_attributes

f.fields_for :contract do |ff| 
1

如果你看看你则params的形状,你会看到organizationcontact_attributes是兄弟,不是父嵌套。这与你的fields_for有关。关闭我的头顶,你所要做的要么是:

= fields_for 'organization[contact_attributes]' do |ff| 

或某种形式的这样:

= fields_for organization.contact do |ff| 

虽然在这种情况下,你显然必须确保它不是零。

或检查@拉蒙的答案,我认为他钉了它。