I'm using Devise, and I have a User
model. By default, there's just a sign up form with an email and a password. I want to add more attributes. For example a college
attribute.
I've been following this blog post to help. I have a RegistrationsController
that overrides the default RegistrationsController
:
class MyDevise::RegistrationsController < Devise::RegistrationsController
def create
super
resource.college = params[:resource][:college]
resource.save
end
end
However, I don't know how to update the user. I've been looking through Devise's RegistrationController , but I can't seem to figure it out. Does it not have access to params[:resource]
from the form?
This is the form in the view file:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %></div>
<div><%= f.label :college %><br />
<%= f.text_field :college %></div>
<% end %>
<%= render "devise/shared/links" %>
Take a look at the Rails and Devise example application from the RailsApps project. For example, to add name
attribute to the User model:
You can create the migration with a generator.
The migration:
# db/migrate/..._add_name_to_users.rb
class AddNameToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
end
end
You need to override the Devise controller to handle strong parameters in Rails 4.0 (and newer).
The controller:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
before_filter :update_sanitized_params, if: :devise_controller?
def update_sanitized_params
devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:name, :email, :password, :password_confirmation)}
devise_parameter_sanitizer.for(:account_update) {|u| u.permit(:name, :email, :password, :password_confirmation, :current_password)}
end
end
The form:
# app/views/devise/registrations/new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
Adjust the routes to accommodate the new controller.
The routes:
# config/routes.rb
RailsDevise::Application.routes.draw do
root :to => "home#index"
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.