使用Devise和Rails访问自定义参数4 [英] Accessing Custom Parameters when using Devise and 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,如果::devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:user){| u | u.permit(:profile_name)}
end
end
而且我已经在前一个问题中建议...
使用Rails 4.0和Devise的强参数
...您可以创建自己的控制器,这可以扩展设计自己的控制器。有一个要点:$ b $ b https://gist.github.com/bluemont/e304e65e7e15d77d3cb9
Devise文档中的一些更多细节: 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
welcome on board. :) Rails is just amazing and you will really enjoy this journey. :)
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 4.0 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
Hope it could help, and let me know what happened.
这篇关于使用Devise和Rails访问自定义参数4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!