Answer the question
In order to leave comments, you need to log in
First argument in form cannot contain nil or be empty. Rails
Hello Habr!
I can't figure out what's wrong. Help me please. Throws an error First argument in form cannot contain nil or be empty . The error occurs when authorizing the user
Logs of the server
Started POST "/users/log_in" for 127.0.0.1 at 2014-04-25 17:38:06 +0400
Processing by UsersController#log_in as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BA7a1ItyjvAJERAdL70Agj3h1j4Y7RvbYHMKe7BvfcI=", "user"=>{"login"=>"mrZaur", "password"=>"[FILTERED]"}, "commit"=>"auth"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."login" = 'mrZaur' LIMIT 1
Rendered users/auth.html.slim (1.7ms)
Completed 500 Internal Server Error in 7ms
ActionView::Template::Error (First argument in form cannot contain nil or be empty):
1: = form_for @user, url: {action: "log_in"} do |f|
2: table
3: tr
4: td = f.label :login
app/views/users/auth.html.slim:1:in `_app_views_users_auth_html_slim___1368928838574431298_11313500'
app/controllers/users_controller.rb:29:in `log_in'
Rendered /home/zaur/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.2ms)
Rendered /home/zaur/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.7ms)
Rendered /home/zaur/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (10.5ms)
= form_for @user, url: {action: "log_in"} do |f| #сюда указывает как на ошибку
table
tr
td = f.label :login
td = f.text_field :login
tr
td = f.label :password
td = f.password_field :password
tr
td = f.submit :auth
def log_in
user = User.find_by login: params[:user][:login]
if user && user.authenticate(params[:user][:password])
session_start
redirect_to_gallery
else
render "auth"
end
end
private
def session_start
session[:user_id] = @user.id
end
def redirect_to_gallery
redirect_to :controller => 'gallery', :action => 'index'
end
Answer the question
In order to leave comments, you need to log in
In the log_in method, you render an auth template that uses the @user variable. But you didn't define it in the method. Therefore, the error.
Do this:
@user = User.find_by login: params[:user][:login]
instead of:
user = User.find_by login: params[:user][:login]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question