【问题标题】:Rails Country_select can't save to databaseRails Country_select 无法保存到数据库
【发布时间】:2015-08-07 16:44:53
【问题描述】:

我无法将此选择国家/地区插件 (https://github.com/scudco/country_select_test) 集成到我的测试应用程序中。我安装了它并将其放入我的数据库中。当我在 Rails 控制台中输入我的 id 时,它会显示出来,但它总是显示为 nil。这是我的 view.haml:

.form-group
        = f.input :country_code, autofocus: true, :class => "form-control"
      .form-group
        = f.input :address, autofocus: false, :class => "form-control"

这是我最近进行的迁移:

class AddCountryCodeToUsers < ActiveRecord::Migration
  def change 
   add_column :users, :country_code, :string
  end
end

我的控制台的输出:

updated_at: "2015-08-07 15:29:26", address: " ", country_code: nil>

我对 users.rb 文件中的 :country_code 一无所知。我曾经在他们的文件中有断言 country_code,但是当我把它拿出来时它并没有改变任何东西。感谢所有帮助我的人。我仍然是 ruby​​ on rails 的菜鸟。

【问题讨论】:

    标签: ruby-on-rails ruby database activerecord


    【解决方案1】:

    由于strong_parameters,您可能错过了在UsersController 中允许:country_code 参数

    在你的控制器中你应该有类似的东西

    def users_params
      params.require(:user).permit(...)
    end
    

    您需要确保在 permit 方法中也调用了 :country_code。例如:

    params.require(:user).permit(:country_code, :email, :password, :password_confirmation)
    

    【讨论】:

    • 你是对的,我没有将它添加到我的控制器中。非常感谢!你太棒了:)
    • 谢谢 ;) 它发生在我身上很多次,我总是在这上面浪费了很多时间。我希望我为您节省了一些时间;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 2015-05-26
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多