0

I'm trying to add country selection in Devise registration and I use country_select gem from https://github.com/stefanpenner/country_select#example

There explains the simple usage by using country_select("user", "country") use model and attribute as parameters:

Problem: When I push submit button user was created and Everything is good except country column doesn't has data from my selection

Purpose: After submit registration I want to insert country which I have selected from Signup form into database(table: users, column: country) also

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

**My model's name is user.rb and in my users table has a country column

Thanks for advance

boated_tw
  • 414
  • 1
  • 9
  • 19

2 Answers2

0

try this out

<%= f.input :country, as: :country %>
djadam
  • 649
  • 1
  • 4
  • 20
0

I have solved my problem by following this Question and adapting to my issue Adding extra registration fields with Devise

I'm creating override controller(Registrations Controller in this case) for allow Devise to adding country variable to database

  1. Create new registrations_controller.rb

I have added :country in this file

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

You can see the original registrations_controller.rb at this link: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

  1. Create a route to allow Rails can routing to override controller by add these lines in routes.rb

devise_for :users, :controllers => { :registrations => 'registrations' }

Important Please make sure that you don't have any devise_for :users line in your routes.rb, If you have, delete it

Well, now I can use <%= country_select("user", "country") %> to save country selection into database without any problem

Community
  • 1
  • 1
boated_tw
  • 414
  • 1
  • 9
  • 19