2016-03-05 71 views
0

我是新來的嵌套窗體。我正在創建一個表單,讓公司可以在幾個類別下創建促銷活動。我一直在爲類定義未定義的方法「model_id」。我的代碼和確切的錯誤消息是如下: -嵌套窗體寶石拋出「未定義的方法」

錯誤消息: -

undefined method `category_id' for #<Company:0x007fd43828b6f8> 
    <%= f.collection_select :category_id, Category.all, :id, :name, {include_blank: 'Choose Category'}, required: true %> 
<%= render partial: 'promo', locals: {f: f}%> 

型號: -

class Company < ActiveRecord::Base 
     has_many :users, :through => :company_customers 
     has_many :company_users 
     has_many :categories, :through => :category_companies 
     has_many :category_companies 

     accepts_nested_attributes_for :category_companies 
     accepts_nested_attributes_for :categories s 
    end 


    class CompanyCategory < ActiveRecord::Base 
     belongs_to :company 
     belongs_to :category 
    end 


    class Category < ActiveRecord::Base 
     has_many :company_categories 
     has_many :companies, :through => :company_categories 
     has_many :promos 

     accepts_nested_attributes_for :company_categories 

     end 

    class Promo < ActiveRecord::Base 
     belongs_to :category 
    end 

控制器: -

 class CompaniesController < ApplicationController 
     def new 
     @company = Company.new 

     end 


     def create 
     @company = Company.new(company_params) 

     respond_to do |format| 
     if @company.save 
     format.html { redirect_to @company, notice: 'Company was successfully created.' } 
     format.json { render :show, status: :created, location: @company } 
     else 
     format.html { render :new } 
      format.json { render json: @company.errors, status: :unprocessable_entity } 
     end 
    end 
     private 
     # Use callbacks to share common setup or constraints between actions. 
    def set_company 
     @company = Company.find(params[:id]) 
    end 


    def company_params 
     params.require(:company).permit(:company_name, :registration_no, :address, :phone_no, :outlet_location, company_categories_attributes: [:id, :company_id, :category_id, category_attributes:[:id, :name, promo_attribute:[:id, :name, :description]]]) 
    end 
    end 

檢視: -

<%= form_for(:company) do |f| %> 
     <div class="medium-6 columns"> 

     <%= f.collection_select :category_id, Category.all, :id, :name, {include_blank: 'Choose Category'}, required: true %> 
     <%= render partial: 'promo', locals: {f: f}%> 
     </div> 

     <div class="actions"> 
     <%= f.submit %> 
    </div> 
    <% end %> 

_promo.html.erb

 <%= f.fields_for :promos do |promo| %>  
     <h4 class="text-center">Promotions to be offered</h4><br> 
     <div class="row"> 
     <div class="medium-6 columns"> 
     <%= f.text_field :name, placeholder: "Name", required: true %> 
     </div> 
     <div class="medium-6 columns"> 
      <%= f.text_field :description, placeholder: "Description", required: true %> 
     </div> 
    </div> 
<% end %> 

    <p><%= f.link_to_add "Add More Promos", :promos %> </p> 

欣賞的幫助。非常感謝 !

回答

0

docs,所述collection_select具有以下用途:

collection_select(對象,方法,收集,value_method,text_method,選項= {},html_options = {})

它們有這樣的例子:

class Post < ActiveRecord::Base 
    belongs_to :author 
end 

class Author < ActiveRecord::Base 
    has_many :posts 
    def name_with_initial 
    "#{first_name.first}. #{last_name}" 
    end 
end 

這將是這樣使用:

collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) 

所以,當你這樣做:

f.collection_select :category_id, Category.all, :id, :name, {include_blank: 'Choose Category'}, required: true 

我想,也許你錯過了object說法?就像,他們有:post

相關問題