Rails:LoginController中的NameError #create

时间:2017-08-17 21:45:06

标签: ruby-on-rails controller

我正在关注为我的网站进行注册/登录的教程。页面登录/新是好的,直到你按提交,一旦我按提交我得到一个错误:

undefined local variable or method login for #LoginController:0x007f8cf74e6f48 Did you mean? login_url

我环顾四周,无法弄清楚我做错了什么,这是我的所有代码:

的routes.rb

  resources :login, only: [:new, :create, :destroy]

login_controller.rb

class LoginController < ApplicationController
  def new
  end

  def create
     user = User.find_by(email: params[:email])
     if user && user.authenticate(params[:password])
       login[:user_id] = user.id
       redirect_to home_url, notice: "Logged in!"
     else
       render "new"
     end
   end

   def destroy
     login[:user_id] = nil
     redirect_to home_url, notice: "Logged out!"
   end
 end

new.html.erb

<h1>Log in</h1>

<%= form_tag login_path do %>
  <div class="field">
    <%= label_tag :email %><br/>
    <%= text_field_tag :email, params[:email] %>
  </div>
  <div class="field">
    <%= label_tag :password %><br/>
    <%= password_field_tag :password %>
  </div>
  <div class="actions"><%= submit_tag "Log in" %></div>
<% end %>

微量

app/controllers/login_controller.rb:8:in `create'
actionpack (5.1.3) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.3) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.3) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.3) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.3) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.3) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:46:in `block in serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:834:in `call'
rack (2.0.3) lib/rack/etag.rb:25:in `call'
rack (2.0.3) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.3) lib/rack/head.rb:12:in `call'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.3) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.3) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.1.3) lib/rails/engine.rb:522:in `call'
puma (3.9.1) lib/puma/configuration.rb:224:in `call'
puma (3.9.1) lib/puma/server.rb:602:in `handle_request'
puma (3.9.1) lib/puma/server.rb:435:in `process_client'
puma (3.9.1) lib/puma/server.rb:299:in `block in run'
puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

提前感谢任何人都可以给我的任何帮助,我对rails非常陌生,所以非常感谢。

1 个答案:

答案 0 :(得分:1)

问题是你在控制器中使用了未定义的login变量。您可能希望使用session代替login在会话中存储user.id

class LoginController < ApplicationController
  def new
  end

  def create
     user = User.find_by(email: params[:email])
     if user && user.authenticate(params[:password])
       session[:user_id] = user.id
       redirect_to home_url, notice: "Logged in!"
     else
       render "new"
     end
   end

   def destroy
     session[:user_id] = nil
     redirect_to home_url, notice: "Logged out!"
   end
 end