问题描述
我正在使用 teamtreehouse 教程学习 Rails.本教程使用 3.1,但我正在尝试学习 4.0,因此我遇到了困难,大概是因为使用了强参数的 rails 4 力.我有两个模型,一个用户模型和一个状态模型.我已经使用 devise 为用户创建身份验证,并包含了新参数.它们是:first_name、:last_name 和:profile_name.我已经创建了用户和状态之间的关系.
目前用户使用新参数进行注册;我可以使用例如 current_user.last_name 访问它们.但是,我想在状态索引视图中显示创建帖子的用户的 profile_name(每个用户还没有单独的页面).我想使用
来做到这一点status.user.profile_name
但是它只是显示为空白.如果我做status.user.email(这是一个预先配置的设计参数),它显示没有问题.我猜我必须在某些控制器中将这些参数列入白名单,但我不知道在哪里或如何.谢谢
我想,在这里你会找到答案:https://github.com/plataformatec/devise/tree/rails4#strong-parameters
基于上面的链接,我认为您应该在 ApplicationController 中插入这样的内容:
class ApplicationController
而且我已经在上一个问题中提出了建议...Rails 和 Devise 的强参数>
...您可以创建自己的控制器,该控制器可以扩展设计自己的控制器.有一个要点:https://gist.github.com/bluemont/e304e65e7e15d77d3cb9
设计文档中的更多细节:https://github.com/plataformatec/devise/tree/rails4#configuring-controllers
I am learning rails using the teamtreehouse tutorial. The tutorial uses 3.1 but I am trying to learn 4.0, and as a result I have run into a difficulty presumably because rails 4 forces of the use of strong parameters. I have two models, a users model and a statuses model. I have used devise to create authentication for users, and have included new parameters. They are :first_name, :last_name, and :profile_name. I have created a relationship between users and statuses.
Currently the user sign-up with the new parameters is working; i can access them using for instance current_user.last_name. However, I want to show the profile_name of the user that created the post on the statuses index view(each user does not yet have a separate page). I want to do this using
status.user.profile_name
However it just shows up blank. If I do status.user.email(which is a preconfigured devise parameter), it shows up no problem. I am guessing I have to whitelist these parameters in some controller but I don't know where or how. Thanks
I think, here you will find your answer: https://github.com/plataformatec/devise/tree/rails4#strong-parameters
Based on above link, I think you should insert something like this in your 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
And I already suggested in a previous question...Strong parameters with Rails and Devise
...that you can create your own controller which could extend devise own controller. There is a gist for that:https://gist.github.com/bluemont/e304e65e7e15d77d3cb9
A little bit more details in Devise doc: https://github.com/plataformatec/devise/tree/rails4#configuring-controllers
这篇关于使用 Devise 和 Rails 4 时访问自定义参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!