我正在尝试使用Rails应用程序 - 我正在使用Devise进行用户登录,我正在尝试将其设置为每次用户成功登录时,特定条目会自动插入到名为{{1的模型中}}
在我的Devise会话控制器中,我有以下与成功用户登录相关的操作:
reports
所以,如果不清楚,我的意思是每次用户登录时在def create
self.resource = warden.authenticate!(auth_options)
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
end
def after_sign_in_path_for(resource)
Report.create(report_params({"comment" => "user logged in"}))
end
private
def report_params(params)
params.permit(:comment)
end
创建一个新条目,该条目将始终显示为“用户已登录”。
但是,当我尝试运行它时,我收到以下错误:
reports
我意识到这个确切的操作可能看起来有点无意义或多余 - 正如我所说,我只是在尝试,但是正确的做法可能会帮助我重构我正在研究的不同应用程序(考虑JSON到位)这个固定的条目)。
如果有人能够解释这个错误的含义并帮助我弄清楚我做错了什么(或者如果这是完成这项任务的最佳方式),那将非常感激。
答案 0 :(得分:1)
您只需在创建操作
中添加此内容即可def create
self.resource = warden.authenticate!(auth_options)
Report.create(comment: "user logged in")
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
end
所以在资源经过身份验证后很快就会在每个sign_in之后创建一个新报告。如果您需要更多
,请在下方发表评论答案 1 :(得分:1)
@Chirag Arya 提供的答案应该适合您。我将专注于解释错误。
未定义的方法`permit'用于{“comment”=>“用户已登录”}:哈希
问题是应permit调用ActionController::Parameters,但您是在 哈希 上调用它。请注意,您使用params
方法中的哈希({"comment"=>"user logged in"}
)覆盖了普通report_params
。
h = {"comment"=>"user logged in"}
=> {"comment"=>"user logged in"}
h.permit
NoMethodError: undefined method `permit' for {"comment"=>"user logged in"}:Hash
a = ActionController::Parameters.new({"comment"=>"user logged in"})
=> {"comment"=>"user logged in"}
a.permit(:comment)
=> {"comment"=>"user logged in"}
另外,我注意到你错误地使用 after_sign_in_path_for(resource)
,一个 Devise帮助方法,这意味着服务器有不同的用途。 See why this method is used