I making a simple login validation and I want to validate the name and the password.
finances_controller .rb
def login
@user = User.find_by_name(params[:user])
if @user
session[:user_id] = @user.id
redirect_to :action => 'index'
end
end
login.html.erb
<h1>Login</h1>
<%= form_for @user do |f| %>
Nome: <%= f.text_field :name %>
Senha: <%= f.password_field :password %>
<%= f.submit %>
<% end %>
This is the error:
undefined method `model_name' for NilClass:Class
Extracted source (around line #2):
1: <h1>Login</h1>
2: <%= form_for @user do |f| %>
3: Nome: <%= f.text_field :name %>
4: Senha: <%= f.password_field :password %>
5: <%= f.submit %>
model
user.rb
class User < ActiveRecord::Base
attr_accessible :name, :password, :password_digest
end
ps: the digest is to make this secure.
Can anybody provide any inputs?
seems like you’re searching a user by her name, but giving a hash consisted of user attributes as an argument (
params[:user]). I think that’s the reason@userremainsniland this error is shown.could you check if changing the argument to
params[:user][:name]gives better result?