2017-07-07 57 views
0

在寶石的實現:https://github.com/arvindvyas/Country-State-Select如何解決Country-State-選擇gem未定義的方法問題?

當我去張貼招聘新的頁面顯示此錯誤的日誌

undefined method `country' for #<Job:0x007f8c07092320> 

我試圖把

accepts_nested_attributes_for 

工作模式,但沒有奏效。

有人有任何提示空閒?

工作模式

class Job < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :location 
    accepts_nested_attributes_for :location, allow_destroy: true 
    default_scope -> { order(created_at: :desc) } 
end 

選址模型

class Location < ActiveRecord::Base 
    has_many :users 
    has_many :jobs 
end 

招聘新觀點

<%= simple_form_for @job do |f| %> 
    <%= f.simple_fields_for :location do |l| %> 
    <%= l.input :country, label: "Country", collection: CountryStateSelect.countries_collection %> 
    <%= l.input :state, CountryStateSelect.state_options(label: "State/Province", form: f, field_names: { :country => :country, :state => :state }) %> 
    <%= l.input :city, CountryStateSelect.city_options(label: "City ", form: f, field_names: { :state => :state, :city => :city }) %>  
    <% end %> 
    <%= f.input :name, label: 'Name', error: 'Name is mandatory' %> 
    <%= f.input :description, placeholder: '[email protected]' %> 
    <%= f.button :submit %> 
<% end %> 
+0

一個simple_fields_for塊內什麼如果您在狀態和城市輸入行中將'form:f'更改爲'form:l',會發生什麼情況? –

+0

謝謝你簡單的石灰,我沒有注意到那個錯誤!現在工作。非常感謝你,如果你想創建答案這個問題讓我接受,那麼確定 – user8256239

+0

確定的事情,不想讓它成爲一個答案,因爲我不知道它是否會有所幫助或者是否只是一個幸福的巧合。 –

回答

0

在田裏

<%= l.input :state, CountryStateSelect.state_options(label: "State/Province", form: f, field_names: { :country => :country, :state => :state }) %> 
<%= l.input :city, CountryStateSelect.city_options(label: "City ", form: f, field_names: { :state => :state, :city => :city }) %> 

你有形式:F,但你這是目前使用的l代替f

+0

簡單的石灰非常感謝你! – user8256239

0

看來你錯過在Job模型中輸入country字段。使用遷移將它添加:

add_column :jobs, :country, :string 
+0

謝謝你Ryan,但是這些字段來自Location,爲什麼Job需要Country字段? – user8256239

相關問題