0

「Store」というモデルがあり、「State」が関連付けられた属性「City」があります。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)、 attr_accessible オプションの代わりに、フォームから属性を割り当てることができるように定義するために使用することをお勧めします

于 2013-03-03T21:34:21.307 に答える