Here is my unaltered controller:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
And here are my routes:
root :to => "songs#index"
match '/votes/:song_id/:user_id' => "votes#create"
resources :votes
resource :session
resources :users
resources :songs
match '/login' => "sessions#new", :as => "login"
match '/logout' => "sessions#destroy", :as => "logout"
And the error:
undefined method `user' for #<User:0x00000102b42a00>
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:46:in `block in create'
app/controllers/users_controller.rb:45:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"oOzmpsbEJtnHC4YGeAf4N6pVxfK+Zf4W9ec+0E/Eds0=",
"user"=>{"email"=>"bhjjhb@hui.com",
"password"=>"[FILTERED]"},
"commit"=>"Create User"}
model:
class User < ActiveRecord::Base
attr_accessible :email, :password
validates_uniqueness_of :user
validates_presence_of :password
has_many :votes
end
http://guides.rubyonrails.org/active_record_validations_callbacks.html#uniqueness
Your problem is in your validations;
validates_uniqueness_ofchecks the uniqueness of a model attribute, sovalidates_uniqueness_of :useris trying to check that the user’suserattribute is unique. In the process, it calls@user.user, which produces theNoMethodError.Edited to add: As @Amar says, the way to fix this is by validating the uniqueness of some attribute or set of attributes that will be unique for each user record (such as
:email).