如何解决 Country-State-Select gem 未定义方法问题?

How to fix Country-State-Select gem undefined method issue?

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

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

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 %>

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

add_column :jobs, :country, :string

在田野里

<%= 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