【问题标题】:Devise and country_select gem don't save selection into database Rails4Devise 和 country_select gem 不会将选择保存到数据库 Rails4
【发布时间】:2015-04-16 08:47:33
【问题描述】:

我正在尝试在设计注册中添加国家/地区选择,我使用来自https://github.com/stefanpenner/country_select#example 的 country_select gem

这里解释了使用country_select("user", "country")使用模型和属性作为参数的简单用法:

问题:当我按下提交按钮时,用户被创建并且一切都很好,除了国家列没有我选择的数据

目的:提交注册后,我想将我从注册表单中选择的国家/地区插入数据库(表:用户,列:国家/地区)

sign_up.html.erb

<h2><center>Sign up</center></h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
   <%= f.error_notification %>

   <div class="form-inputs" style="float; margin:0 auto;width:35%">
     <%= f.input :email, required: true, autofocus: true %>
     <%= f.input :password, required: true %>
     <%= f.input :password_confirmation, required: true %>
     <%= f.label :country %>

     <%= country_select("user", "country") %>  <<-- My model's name is user.rb and in my users table has a country column

   </div>

   <div class="form-actions" style="float; margin:0 auto;width:10%">
     <%= f.button :submit, "Sign up" %>
   </div>
<% end %>

**我的模型名称是 user.rb,在我的 users 表中有一个国家列

感谢提前

【问题讨论】:

  • 看起来你的控制器有问题。结帐:stackoverflow.com/questions/16471498/…
  • @jonsnow 我正在关注你的链接,现在我已经解决了,我应该发布这个问题的答案
  • 为什么不呢,把它贴出来给你的问题增加更多的价值。 ;)

标签: mysql ruby-on-rails ruby devise


【解决方案1】:

试试这个

<%= f.input :country, as: :country %>

【讨论】:

    【解决方案2】:

    我已经通过关注这个问题并适应我的问题Adding extra registration fields with Devise解决了我的问题

    我正在创建覆盖控制器(在这种情况下为注册控制器)以允许设计将国家/地区变量添加到数据库

    1. 新建registrations_controller.rb

    我在这个文件中添加了:country

    class RegistrationsController < Devise::RegistrationsController
        before_filter :configure_permitted_parameters, :only => [:create]
    
        protected
    
        def configure_permitted_parameters
            devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :country) }
        end
    end
    

    您可以在此链接查看原始registrations_controller.rb:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

    1. 创建一个路由以允许 Rails 可以通过添加这些行来覆盖控制器 在routes.rb

    devise_for :users, :controllers =&gt; { :registrations =&gt; 'registrations' }

    重要提示请确保您的routes.rb 中没有任何devise_for :users 行,如果有,请删除它

    好吧,现在我可以使用&lt;%= country_select("user", "country") %&gt; 将国家选择保存到数据库中,没有任何问题

    【讨论】:

    • 接受答案,让人们知道,你已经解决了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多