2016-03-05 81 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

相关问题