使用Devise和Rails 4访问自定义参数
我正在学习使用teamtreehouse教程的rails。本教程使用3.1,但我试图学习4.0,因此我遇到了困难,可能是因为rails 4强制使用强参数。我有两个模型,一个用户模型和一个状态模型。我用devise为用户创建了认证,并且包含了新的参数。它们是:first_name,:last_name和:profile_name。我已经创建了用户和状态之间的关系。使用Devise和Rails 4访问自定义参数
当前用户使用新参数注册正在工作;我可以使用例如current_user.last_name来访问它们。但是,我想要显示在状态索引视图中创建帖子的用户的profile_name(每个用户还没有单独的页面)。我想这样做使用
status.user.profile_name
但它只是显示空白。如果我做 status.user.email(这是一个预先配置的设计参数),它显示没有问题。我猜我必须在某些控制器中将这些参数列入白名单,但我不知道在哪里或如何。 谢谢
欢迎在船上。 :) Rails是惊人的,你会真正喜欢这个旅程。 :)
我想,在这里你会找到答案:https://github.com/plataformatec/devise/tree/rails4#strong-parameters
基于上面的链接,我想你应该在你的ApplicationController插入这样的事情:
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:user) { |u| u.permit(:profile_name) }
end
end
而且我已经建议以前的问题... Strong parameters with Rails and Devise
...你可以创建自己的控制器,可以扩展设计自己的控制器。没有为一个要点是:更 https://gist.github.com/bluemont/e304e65e7e15d77d3cb9
一点点细节设计DOC:https://github.com/plataformatec/devise/tree/rails4#configuring-controllers
希望它可以帮助,让我知道发生了什么。
它在我切换时工作:用户:sign_up。从我的阅读:如果我重写了控制器中的user_params方法,用户将工作。那就是我想避免的......(我应该学会如何,因为如果没有这样做,设计的真正定制就不可能实现)。 – kempchee