Rails + Authlogic @current_user问题 – “显示”方法不再有效
我在我的rails应用程序中实现了Authlogic,它的工作和测试非常好。 用户可以登录,注销等
但是,由于我在users_controller中更改了我的方法以使用@current_user变量,因此我的用户方法或表单(除了“索引”奇怪的……)都不起作用!
例如,这个“显示”方法可以工作并显示任何用户的用户配置文件(注意:我想最终以某种方式将此限制为当前登录的用户):
def show @user = User.find(params[:id]) end
如果我按照Authlogic rails cast和官方示例中所示的show方法执行:
def show @user = @current_user end
我得到各种undefined method METHOD for nil:NilClass
错误。 尽管@current_user变量不包含任何用户信息。
我假设@current_user从current_user方法中提取信息,如下面的应用程序控制器中所定义:
def current_user return @current_user if defined?(@current_user) @current_user = current_user_session && current_user_session.record end
我错过了什么? 我的预感是用户模型的User对象和current_user方法之间没有映射。 任何帮助将不胜感激!
提前致谢!
〜丹
不应该只是
def show @user = current_user end
没有@
?
current_user是一个authlogic附带的帮助器,所以你必须在没有’@’的情况下使用它。 通常您创建@sothing以将其从控制器传递到视图,因此您将访问在action方法中准备的内容。
我在authlogic和rails 3上遇到了类似的问题,这是因为我在生产服务器上使用了apache http身份validation。 我通过将以下行添加到我的user_session.rb(模型)来解决这个问题
allow_http_basic_auth false
这是保存我生命的博客链接http://www.christopherirish.com/2011/08/12/rails-3-authlogic-sessions-not-persisting-in-production/comment-page-1/#comment -181
def if @current_user @user = @current_user else @user = User.find(params[:id]) end end
只是一个猜测,试一试。