Mark M
Mark M

Reputation: 91

undefined local variable or method `user'

I am getting this error: undefined local variable or method `user' for # which points to the profiles controller line 9, "First line after def update method" BTW I am using Devise for my User.

class ProfileController < ApplicationController
before_action :authenticate_user!

def index
  @user = current_user
end

def update
  current_user.update(user.params) 
  redirect_to root_path
end


private
  def user_params
  params.require(user).permit(:first_name, :last_name)
  end
end

Here is my application.html.erb where my editor "Rubymine" tells me that user_signed_in?, current_user, destroy_user_session_path, new_user_session_path, and new_user_registration_path CANNOT BE FOUND

<!DOCTYPE html>
<html>
<head>
<title>TwitterClone</title>
<%= stylesheet_link_tag    'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
<%= csrf_meta_tags %>
</head>
<body>

<div class="right">
<% if user_signed_in? %>
  Welcome <%= current_user.display_name %>,
    <%= link_to "Logout", destroy_user_session_path, method: "DELETE" %>
<% else %>
  Please
    <%= link_to "Login", new_user_session_path %>
    or
    <%= link_to "Signup", new_user_registration_path %>
<% end %>
</div>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
</html>

My routes are the ones Devise generates for User plus the ones I have for Profile which are:

profile GET    /profile(.:format)             profile#index
        PATCH  /profile(.:format)             profile#update

Here is the schema Devise generated:

ActiveRecord::Schema.define(version: 20160406005549) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "users", force: :cascade do |t|
 t.string   "email",                  default: "", null: false
 t.string   "encrypted_password",     default: "", null: false
 t.string   "reset_password_token"
 t.datetime "reset_password_sent_at"
 t.datetime "remember_created_at"
 t.integer  "sign_in_count",          default: 0,  null: false
 t.datetime "current_sign_in_at"
 t.datetime "last_sign_in_at"
 t.inet     "current_sign_in_ip"
 t.inet     "last_sign_in_ip"
 t.datetime "created_at",                          null: false
 t.datetime "updated_at",                          null: false
 t.string   "first_name"
 t.string   "last_name"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name:    "index_users_on_reset_password_token", unique: true, using: :btree

end

Upvotes: 2

Views: 3239

Answers (1)

Anthony E
Anthony E

Reputation: 11235

There are a few things wrong with your code. For starters you're calling an undefined variable in your controller. user should be a symbol and user.params should be user_params

class ProfileController < ApplicationController
before_action :authenticate_user!

def index
  @user = current_user
end

def update
  current_user.update(user_params) 
  redirect_to root_path
end


private
  def user_params
  params.require(:user).permit(:first_name, :last_name)
  end
end

You also need to update your routes to support the user actions you described:

Within routes.rb:

resources :users

Finally, to use the Devise helper methods make sure you have the Devise routes at the top of your routes.rb as well:

devise_for :users

Upvotes: 3

Related Questions