0

我有一个名为“Store”的模型,它有一个属性“City”,它有一个与之关联的“State”。我正在使用 simple_form 按州和/或城市创建商店过滤器。问题是,如何使 simple_form 不将“State”与“Store”相关联?

class State < ActiveRecord::Base
  attr_accessible :name

  has_many :cities
end

class City < ActiveRecord::Base
  attr_accessible :name

  belongs_to :state
  has_many   :stores
end

class Store < ActiveRecord::Base
   attr_accessible :latitude, :longitude, :description, :city_id

   validates :city,        :presence => true
   validates :description, :presence => true, :length => {:maximum => 500}
   validates :latitude,    :presence => true
   validates :longitude,   :presence => true

   belongs_to :city
end


<%= simple_form_for @store, :html => { :class => 'add-store-form', :style => "display:none;" } do |f| %>
  <table border="0">
    <tr>
      <td>Estado:</td>
      <td>
        <%= f.collection_select :state, State.all, :include_blank => false, :label => false, 
                              :input_html => { :id => "state_id", :name => "state_id" } %>
      </td>
    </tr>
    .
    .

这种方式行不通。我怎样才能做到这一点?

提前致谢!

4

1 回答 1

1

这不是您问题的答案,而是通过以下方式改进您的代码:

validates :city, :latitude, :longitude, :description, presence: true
validates :description, length: {maximum: 500}

我建议使用Strong Parameters (Default in rails 4)to define with attributes can be assigned from forms而不是 attr_accessible 选项

于 2013-03-03T21:34:21.307 回答