0

I have created a simple Rails app, that uses devise. For logging in and out I use the standard USER model that has email + password. I have a PROFILE model to store all the other details for the user.

I want to create a page where the user can update all their details. Currently getting the following error...

NoMethodError in UsersController#update

undefined method `name' for nil:NilClass

Screenshot of entire error message

My models look as so...

class User < ApplicationRecord
    Include default devise modules. Others available are:
    :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
    devise :database_authenticatable, :registerable, :recoverable,  :rememberable, :validatable
             
      has_one :profile
      after_create :create_profile
      attr_accessor :first_name, :last_name, :description
    
      accepts_nested_attributes_for :profile
    
    end
    
        class Profile < ApplicationRecord
         belongs_to :user
        end

View page code looks like...

<h1>Account Details</h1>

<p><strong>Email:</strong>    <%= @user.email %></p>
<p><strong>First Name:</strong> <%= @user.profile.first_name %></p>
<p><strong>Last Name:</strong> <%= @user.profile.last_name %></p>

<%= form_for(@user, :as => :user) do |field| %>
<p>
<%= field.label :email  %>
<%= field.text_field :email %>
</p>
<%= fields_for @user.profile do |profile| %>
  <p>
    <%= profile.label :first_name %><br>
    <%= profile.text_field :first_name %>
    <% end %>
  </p>
  <p>
  <%= field.submit "Update" %>
  </p>
<% end %>

and the Controller looks like

    Class UsersController < Devise::RegistrationsController

    def new
        build_resource({})
        resource.build_profile
        respond_with self.resource
    end

    def create
        super
    end

    def edit
        super
    end

    def update
        @user = current_user
    end

    def show
        @user = current_user
    end

    before_action :configure_permitted_parameters, if: :devise_controller?

 protected

 def configure_permitted_parameters
   devise_parameter_sanitizer.permit(:sign_up) { |u|
     u.permit(:email, :password, [profile_attributes: [ :first_name, :last_name]])
}
end 

end

I can't get this form to render and update the models. I hope someone can tell me what I am doing wrong.

2 Answers 2

1

Try this:

  • move configure_permitted_parameters and corresponding before_action to the ApplicationController
  • delete UsersController, looks like you don't need it
  • change devise permitted parameters a bit: u.permit(:email, :password, profile_attributes: [:id, :first_name, :last_name])
Sign up to request clarification or add additional context in comments.

5 Comments

Moved parameters as advised. Still needed the Users controller. Not really sure why.
I don't remember exactly. It was a routing error saying that UsersController could not be found... even though I had removed all reference. On relection maybe I should have destroyed it properly via a rails d controller user command.
@JimmyD, maybe you forgot to remove it from routes.rb?
I definitely removed it from the routes.rb file... but as I created it via a rails command (g controller) I'm thinking that I should have removed it via the rails destroy command. What I actually did was just delete the file manually
@JimmyD, it should be enough to delete the file. If you want, you can remove it again and create a new question on SO (with full error trace and routes.rb), I'll try to help you
0

delete UsersController and

in application_controller

class ApplicationController<ActionController::Base
  protect_from_forgery with::exception
  before_action:configure_permitted_parameters,if::devise_controller? 

def check_is_admin
 if current_user.is_admin
  return true
 else
   redirect_to roots_path,notice:"page looking for sign_in"
 end
end

private

def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end

def after_sign_in_path_for(resource_or_scope)
root_path
end

protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up,keys:[:first_name, :last_name])
devise_parameter_sanitizer.permit(:account_update,keys:[:first_name, :last_name])
end

end

you can change path depend on your use....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.