2017-07-07 58 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

相关问题