Reputation: 2127
I am trying to update only email using this form, but validation is failing Current password can't be blank
, requires password_confirmation, I don't have a clue how to void this requirement
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:method => :put, :id=>"email_form"}) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %>
<br/>
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.submit "Update" %></div>
<% end %>
UPDATE This is my update action in the overrided controller RegistrationsController < Devise::RegistrationsController
class RegistrationsController < Devise::RegistrationsController
def update
@user = User.find(current_user.id)
successfully_updated = if needs_password?(@user, params)
@user.update_with_password(params[:user])
else
# remove the virtual current_password attribute update_without_password
# doesn't know how to ignore it
params[:user].delete(:current_password)
@user.update_without_password(params[:user])
end
if successfully_updated
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
private
def needs_password?(user, params)
user.email != params[:user][:email] ||
params[:user][:password].present?
end
end
Upvotes: 2
Views: 3421
Reputation: 1613
Just point to other controller than devise's own registration controntroller, with your own logic for this update action.
You can also override devise registration's controller (read devise docs).
Example:
# config/routes.rb
resource :profile # Singular resource and profile
# profiles_controller
def show
# render your view with the for
end
def update
current_user.update(params[:user]) # if rails < 4 use update_attributes instead
end
# _form.html.erb
<%= form_for(current_user, url: profile_path, html: { method: 'PUT' }) do |f| %>
...
For the second option, overriding devise's own registration controller, I don't really like this approach because in this case you are not really dealing with registrations but an already registered user account:
After your edit:
I see you are using the second option. Take a look a the needs_password? method in the controller
Upvotes: 1