1
votes

I am using rails 4 and is currently going through this tutorial

https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password.

I am trying to update an user record without having the user input their password/password confirmation. I am mainly using the first example from the documentation.

here is my routes.rb (setting the devise to user my customize registrations controller)

devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks", registrations: "registrations", confirmations: "confirmations"}

here is my application controller (setting parameters for devise)

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

#Catch all CanCan errors and alert the user of the exception
  rescue_from CanCan::AccessDenied do | exception |
    redirect_to root_url, alert: exception.message 
  end

protect_from_forgery

  def not_found(msg="Not Found")
    raise ActionController::RoutingError.new(msg)
  end

  def after_sign_in_path_for(resource_or_scope)
    page_path('chooseapplication')
  end 

  def after_sign_out_path_for(resource_or_scope)
    root_url
  end

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name << :high_school_graduation_year
    devise_parameter_sanitizer.for(:account_update) << :high_school_graduation_year << :country_of_residence << :primary_language << :password << :password_confirmation
  end
end

my registrations controller

class RegistrationsController < Devise::RegistrationsController (note the update method from guide) 
  protected

  def after_sign_up_path_for(resource) 
    page_path('verifyemail')
  end 

  def after_inactive_sign_up_path_for(resource)
    page_path('verifyemail')
  end 

  def update
    # For Rails 4
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)

    # required for settings form to submit when password is left blank
    if account_update_params[:password].blank?
      puts "entered if statement"
      account_update_params.delete("password")
      account_update_params.delete("password_confirmation")
      puts account_update_params.inspect
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(account_update_params)
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end
end

my edit.html.erb (note the method put option to /users)

<div class="container containerWrapper">
  <div class="row">
    <div class="col-xs-12">
      <h3 class="text-left">Create an Account | Step 2 of 2</h3> 
    </div>
    <div class="col-xs-12">
       <h2>High School Graduation:</h2>
           <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
           <%= devise_error_messages! %>
       <div class="formWrap clearfix">
         <div class="col-md-12">
           <div class="form-group form-group-lg">
             <strong><%= f.label :high_school_graduation_year %></strong><br>
             <%= f.number_field :high_school_graduation_year %>
            </div>
        </div>
        <div class="formWrap clearfix">
          <div class="form-group form-group-lg">
            <%= f.label :country_of_residence %><br />
            <%= f.text_area :country_of_residence, :class => "form-control", :rows => "3" %>
          </div>
          <div class="form-group form-group-lg">
            <%= f.label :primary_language %><br />
            <%= f.text_area :primary_language, :class => "form-control", :rows => "3" %>
          </div>
        </div>
          <div><%= f.submit "SIGN-IN", :class => "btn btn-default submitButton center-block" %></div>
        <% end %>
      </div>
    </div>
  </div>

   <div class="col-xs-12 text-center">
    <hr>
    <a class="text-uppercase" href="#">NEED HELP CREATING YOUR ACCOUNT?</a>
   </div>
  </div>
</div>

In my server I am getting put to /users

I am getting this error AbstractController::ActionNotFound (The action 'update' could not be found for RegistrationsController):

When I do rake routes I have these

PATCH    /users(.:format)                                                       registrations#update
                                                PUT      /users(.:format)                                                       registrations#update

I am not seeing my user inputted values in a hash? also I tried commenting out the code inside the update method. I tried adding the super keyword there as well.

1
Where did you find documentation on how to create this controller? I'm struggling trying to find a way to actually register new users for use with devise_token_auth for my API.Riptyde4

1 Answers

2
votes
class RegistrationsController < Devise::RegistrationsController (note the update method from guide) 

  protected
  [snip]    
  def update

The protected directive is hiding the update action. Move the update method above the protected line.