railstutorial.org SessionsController中的NoMethodError #create

时间:2011-04-16 23:36:26

标签: ruby-on-rails-3

undefined method `current_user=' for #<SessionsController:0x1044926b8>
Rails.root: /Users/Bulow/ruby/sample_app
Application Trace | Framework Trace | Full Trace
app/helpers/sessions_helper.rb:4:in `sign_in'
app/controllers/sessions_controller.rb:20:in `create'
Request
Parameters:
{"commit"=>"Sign in",
 "session"=>{"password"=>"[FILTERED]",
 "email"=>"kennyvonbulow@gmail.com"},
 "authenticity_token"=>"/fWZncGEWuhQLIYMxRPXBcBJ37vzLoVrIz1QHU28u6w=",
 "utf8"=>"✓"}
Show session dump
Show env dump
Response
Headers:
None

无法简单登录 - 我不知道如何纠正它。来回遍布指南,似乎无法找到导致此错误的错误

1 个答案:

答案 0 :(得分:1)

在线教程在sessions_helper.rb中有以下定义

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    self.current_user = user
  end
.
.
.
  def sign_out
    cookies.delete(:remember_token)
    self.current_user = nil
  end

我将它们更改为以下内容,并且能够无错误地登录/注销。

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    @current_user = user
  end
.
.
.
  def sign_out
    cookies.delete(:remember_token)
    @current_user = nil
  end

此外,要确认您已登录,请将URL localhost:3000 / users /(您的用户ID)更改为另一个用户ID。它会显示用户信息。点击顶部的个人资料链接,它会返回到您的个人资料。设置应用程序,以便此链接将到达登录用户的配置文件。

我找到了解决方案。 http://getsatisfaction.com/railstutorial/topics/undefined_method_current_user_for_sessionscontroller