Ruby on rails 3 在Rails中向业务模型添加多个类别

Ruby on rails 3 在Rails中向业务模型添加多个类别,ruby-on-rails-3,associations,categories,Ruby On Rails 3,Associations,Categories,我正在尝试设置一个具有业务模型和类别模型的rails应用程序。一个企业可以有多个类别,这些类别可以属于多个企业 一切似乎都正常工作,我没有收到任何错误,除非试图显示与特定业务相关的类别,没有显示任何内容 下面是我的模型、控制器和视图。这是我的第一个rails项目之一,我正在寻求一些帮助 business.rb class Business < ActiveRecord::Base attr_accessible :category_id belongs_to :category

我正在尝试设置一个具有业务模型和类别模型的rails应用程序。一个企业可以有多个类别,这些类别可以属于多个企业

一切似乎都正常工作,我没有收到任何错误,除非试图显示与特定业务相关的类别,没有显示任何内容

下面是我的模型、控制器和视图。这是我的第一个rails项目之一,我正在寻求一些帮助

business.rb

class Business < ActiveRecord::Base

  attr_accessible :category_id
  belongs_to :category

end
class Category < ActiveRecord::Base

  attr_accessible :name, :description
  has_many :businesses

end
def show
  @business = Business.find(params[:id])
  @categories = Category.where(:business_id => @business).all

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @business }
  end
end

def new
  @business = Business.new
  @categories = Category.order(:name)

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @business }
  end
end

def edit
  @business = Business.find(params[:id])
  @categories = Category.order(:name)
end
...

<div class="field">
    <%= f.association :category, input_html: { class: 'chosen-select', multiple: true } %>
</div>

...
...

<ul class="tags">
  <% @categories.each do |category| %>
    <li><%= category.name %></li>
  <% end %>
</ul>

...
business/_-form.html.erb

class Business < ActiveRecord::Base

  attr_accessible :category_id
  belongs_to :category

end
class Category < ActiveRecord::Base

  attr_accessible :name, :description
  has_many :businesses

end
def show
  @business = Business.find(params[:id])
  @categories = Category.where(:business_id => @business).all

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @business }
  end
end

def new
  @business = Business.new
  @categories = Category.order(:name)

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @business }
  end
end

def edit
  @business = Business.find(params[:id])
  @categories = Category.order(:name)
end
...

<div class="field">
    <%= f.association :category, input_html: { class: 'chosen-select', multiple: true } %>
</div>

...
...

<ul class="tags">
  <% @categories.each do |category| %>
    <li><%= category.name %></li>
  <% end %>
</ul>

...
。。。
...
business/show.html.erb

class Business < ActiveRecord::Base

  attr_accessible :category_id
  belongs_to :category

end
class Category < ActiveRecord::Base

  attr_accessible :name, :description
  has_many :businesses

end
def show
  @business = Business.find(params[:id])
  @categories = Category.where(:business_id => @business).all

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @business }
  end
end

def new
  @business = Business.new
  @categories = Category.order(:name)

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @business }
  end
end

def edit
  @business = Business.find(params[:id])
  @categories = Category.order(:name)
end
...

<div class="field">
    <%= f.association :category, input_html: { class: 'chosen-select', multiple: true } %>
</div>

...
...

<ul class="tags">
  <% @categories.each do |category| %>
    <li><%= category.name %></li>
  <% end %>
</ul>

...
。。。
...
鉴于您希望一个企业有多个类别,您的模型的关系应更新如下:

业务

def Business < ActiveRecord::Base
  has_many :categories
  attr_accessible :name, :etc
  # attr_accessible :category_id would not apply as the business model 
  # would not have this relationship
end

您编写的关于模型的内容与代码显示的内容之间似乎存在冲突。您注意到,一个企业可以有许多类别,并且一个类别属于一个企业。然而,您的模型与您所写的相反(一个业务属于一个类别,它有许多业务)。将有助于澄清哪种架构是正确的。@craig.kaminsky我想为一个企业分配多个类别。现在唯一的问题是我无法批量分配受保护的属性(请参见图片:)。我正在尝试从“业务/编辑”视图中为每个业务设置类别。您应该能够将
:category\u id
添加到业务模型中的
attr\u accessible
表达式中。假设您使用的是Rails 3.x(看起来是这样的),这就成功了!非常感谢克雷格的耐心和支持!