ruby on rails - Custom fields on Devise and Mongoid embedded -



ruby on rails - Custom fields on Devise and Mongoid embedded -

i'm trying create custom fields on devise sign process embeds info other collection. want embeds 1 country user:

here user.rb model

class user include mongoid::document include mongoid::attributes::dynamic include mongoid::paperclip # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable ## database authenticatable field :email, type: string, default: "" field :encrypted_password, type: string, default: "" ## recoverable field :reset_password_token, type: string field :reset_password_sent_at, type: time ## rememberable field :remember_created_at, type: time ## trackable field :sign_in_count, type: integer, default: 0 field :current_sign_in_at, type: time field :last_sign_in_at, type: time field :current_sign_in_ip, type: string field :last_sign_in_ip, type: string field :unconfirmed_email, type: string field :confirmation_token, type: string field :confirmed_at, type: datetime field :confirmation_sent_at, type: datetime # info field :username, type: string embeds_one :country end

here country.rb model

class country include mongoid::document field :name, type: string embedded_in :user validates_presence_of :name end

here application controller strong params:

class applicationcontroller < actioncontroller::base # prevent csrf attacks raising exception. # apis, may want utilize :null_session instead. protect_from_forgery with: :exception before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_in) {|u| u.permit(:signin)} devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email, :username, :password, :password_confirmation, :country => {})} end def after_sign_in_path_for(resource) request.env['omniauth.origin'] || stored_location_for(resource) || profile_path end end

and devise/registrations/new.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) |f| %> <%= devise_error_messages! %> <%= f.text_field :username, autofocus: true, id: "username", placeholder: :name } %> <%= f.email_field :email, autofocus: true, id: "email", placeholder: :email } %> <%= f.password_field :password, autocomplete: "off", id: "password", placeholder: :password } %> <%= f.password_field :password_confirmation, autocomplete: "off", id: "confirm_password", placeholder: :password_confirmation } %> <%= f.collection_select(:country, country.all, :id, :name, {prompt: "select country",:include_blank => "please select"}, {class: "form-control", id: "country"}) %> <%= f.submit "sign up", class:"submit action-button" %> <% end %>

most curious country param correctly send, devise destroy it, @ log file:

processing users::registrationscontroller parameters: {"utf8"=>"✓", "authenticity_token"=>"nyx6oc3nvdl2h54kn+1mottxdqz+klsccao7rref3nq=", "user"=>{ "username"=>"test", "email"=>"test@test.net", "password"=>"[filtered]", "password_confirmation"=>"[filtered]", "country"=>"5395a3db6d696e0d32080000"}, "commit"=>"sign up"} moped: 127.0.0.1:27017 insert database=app_development collection=users documents=[ {"_id"=>bson::objectid('53a806cf6d696e04a2040000'), "email"=>"test@test.net", "encrypted_password"=>"$2a$10$wiglimkqpq2k7bygo.b0wume7u2jugpqlhpycdq1sdjxyxjdvpu12", "sign_in_count"=>0, "username"=>"test", "confirmation_token"=>"95dfa85e84a37405a74ce005277c8be79df954645f9515fdb06903c75f3d2772", "confirmation_sent_at"=>2014-06-23 10:51:59 utc}] flags=[]

am missing something? thanks

you have add together take attributes property in model take embedded document fields

in user model

embeds_one :country accepts_nested_attributes_for :country

in forms utilize fields_for country , in application_controller set sanitized parameters as

devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email, :username, :password, :password_confirmation, :country_attributes => {:name})}

ruby-on-rails devise mongoid field

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -