Accessing Custom Parameters when using Devise and Rails 4

岁酱吖の 提交于 2020-01-11 04:08:05

问题


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


回答1:


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 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.



来源:https://stackoverflow.com/questions/17735639/accessing-custom-parameters-when-using-devise-and-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!