如何修复Country-State-Select gem undefined方法问题?

时间:2017-07-07 00:55:14

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

在gem的实现中:https://github.com/arvindvyas/Country-State-Select

当我转到Post Job新页面时,在日志中显示此错误

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: 'user@domain.com' %>
  <%= f.button :submit %>
<% end %>

2 个答案:

答案 0 :(得分:0)

您似乎错过了country模型中的Job字段。使用迁移添加它:

add_column :jobs, :country, :string

答案 1 :(得分: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 ,但您位于simple_fields_for区块内,目前正在使用l而不是f