rails如何在视图页面上获取相关的id

时间:2014-05-29 19:24:46

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我试图让这个有条件:

<% if current_boutique.boutique_kind(3) %>
  Brand: <span><%= current_boutique.name %></span>

  <% else %>
<%= p.input :brand_id, :as => :select, :collection=> Brand.find(:all, :order=>:name).collect{ |b| [b.name,b.id,  b.name]},
  :label => "Marca", :prompt => 'Select Brand', :required => true  %>

<% end %>

boutique_kind(3)显示所有精品店!我如何通过boutiques_kind id 3方法获得精品店!

boutiqueKind Controller

def show 
  @boutique_kind = BoutiqueKind.find(params[:id])
end

精品店模型

 attr_accessible :kind, :slug

  has_many                      :boutiques
  has_many                      :products, :through => :boutiques
  #belongs_to                    :gender
  accepts_nested_attributes_for :boutiques
  attr_accessible               :boutiques, :boutiques_attributes, :kind

精品控制器

class Boutique < ActiveRecord::Base
  belongs_to :user
  belongs_to :boutique_kind
end

1 个答案:

答案 0 :(得分:1)

您应该可以通过ID找到精品店:

BoutiqueKind.find(3)

如果我误解了,并且id不是唯一的id(在这种情况下我建议你将列重命名为id以外的其他内容),你可以获得某个字段==特定值的所有记录:< / p>

BoutiqueKind.where(id: 3)