2016-02-19 71 views
1

當我嘗試編輯聯繫人以添加DataType時,我收到:'未經許可的參數:data_format'錯誤。我嘗試過以下類似的聯繫,但我無法弄清楚我缺少的東西。如果這是多餘的,我很抱歉。何處允許has_one/belongs_to關聯中的參數?

這裏是信息的相關部分:

型號:

# == Schema Information 
# 
# Table name: contacts 
# 
# id   :integer   not null, primary key 
# name  :string 
# email  :string 
# phone  :string 
# mobile  :string 
# created_at :datetime   not null 
# updated_at :datetime   not null 
# supplier_id :integer 
# 
class Contact < ActiveRecord::Base 
    has_one :data_type 
    accepts_nested_attributes_for :data_type, allow_destroy: true 
end 
# == Schema Information 
# 
# Table name: data_formats 
# 
# id   :integer   not null, primary key 
# name  :string 
# created_at :datetime   not null 
# updated_at :datetime   not null 
# contact_id :integer 
# 
class DataFormat < ActiveRecord::Base 
    belongs_to :contact 
end 

聯繫控制器:

class ContactsController < ApplicationController 
    before_action :set_contact, only: [:show, :edit, :update, :destroy] 

    def new 
    @contact = Contact.new 
    end 

    def create 
    @contact = Contact.new(contact_params) 

    respond_to do |format| 
     if @contact.save 
     format.html { redirect_to @contact, notice: 'Contact was successfully created.' } 
     format.json { render :show, status: :created, location: @contact } 
     else 
     format.html { render :new } 
     format.json { render json: @contact.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    private 

    def set_contact 
     @contact = Contact.find(params[:id]) 
    end 

    def contact_params 
     params.require(:contact).permit(:name, :email, :phone, :mobile, :data_format, 
             data_format_attributes: [ :id, :name, :contact_id, :_destroy ]) 
    end 
end 

編輯觀點:

<%= simple_form_for @contact do |f| %> 
    <div class="form-group"> 
    <div class="col-lg-6"> 
     <%= f.input :name %> 
    </div> 
    <div class="col-lg-6"> 
     <%= f.simple_fields_for :data_format do |t| %> 
     <%= t.input :id, label: 'Date Format:', :collection => DataFormat.order(:name) %> 
     <% end %> 
    </div> 
    <div class="col-lg-6"> 
     <%= f.label "Submit", class: 'control-label' %> 
     <%= f.button :submit, class: 'btn btn-primary form-control' %> 
    </div> 
    </div> 
<% end %> 

DataType(s)出現在窗體中,我可以選擇它們。所以,我知道這個問題必須在控制器內。

任何幫助將不勝感激!

回答

1

您的contact.rb正在接受名稱不匹配的嵌套屬性名稱和permitted parameterscontact.rb提到:accepts_nested_attributes_for :data_type, allow_destroy: true .

contact.rb

class Contact < ActiveRecord::Base 
    has_one :data_format 
    accepts_nested_attributes_for :data_format, allow_destroy: true 
end 

contacts_controller.rb

class ContactsController < ApplicationController 
    before_action :set_contact, only: [:show, :edit, :update, :destroy] 

    def new 
    @contact = Contact.new 
    @contact.data_format = DataFormat.new 
    end 

    ... 

    private 

     def set_contact 
      @contact = Contact.find(params[:id]) 
     end 

     def contact_params 
      params.require(:contact).permit(:name, :email, :phone, :mobile, :data_format, 
              data_format_attributes: [ :id, :name, :contact_id, :_destroy ]) 
     end 

形式

<%= simple_form_for @contact do |f| %> 
    <div class="form-group"> 
    <div class="col-lg-6"> 
     <%= f.input :name %> 
    </div> 
    <div class="col-lg-6"> 
     <%= f.simple_fields_for :data_format, @contact.data_format do |t|%> 
     <%= t.input :id, label: 'Date Format:', :collection => DataFormat.order(:name) %> 
     <% end %> 
    </div> 
    <div class="col-lg-6"> 
     <%= f.label "Submit", class: 'control-label' %> 
     <%= f.button :submit, class: 'btn btn-primary form-control' %> 
    </div> 
    </div> 
<% end %> 
+0

嘿!感謝您的答覆!完成這些更正後,我的輸入字段消失。有什麼建議麼? – nkulig

+0

您還需要在''new'''動作中'''build''',以便從中加載表單的對象實例。我已經更新了答案。看看是否有幫助。 – shayonj

+0

嘿!我也偶然發現了這一點。但是,輸入仍然沒有顯示出來。 – nkulig