In rails 4, how can I manipulate strong parameters before creation?

我是研究僧i 提交于 2019-12-11 00:38:02

问题


I am trying to convert an array inputed from a form multiple select field into a string separating the elements with commas. The gem i am using requires a tag_field in the form of a string separated by commas, but my multiple select field creates an array. Strong parameters reject the array so I need to convert the array to a string. Here is the code I have now in my application controller, but it is not working.

def configure_devise_params
    devise_parameter_sanitizer.for(:sign_up) do |u|
        u[:tag_list].join(', ')
        u.permit(:email, :password, :password_confirmation,
        :profile_name, :how_did_you_hear, :first_name, :last_name, :type, :tag_list)
    end
end 

Before I was adding :tag_list, I had the following code that worked:

def configure_devise_params
    devise_parameter_sanitizer.for(:sign_up) do |u|
        u.permit(:email, :password, :password_confirmation,
        :profile_name, :how_did_you_hear, :first_name, :last_name, :type)
    end
end 

How do I fix this? Thanks.


回答1:


There is a simple way to permit array.

Example: params.require(:article).permit(:title, {:rubric_ids => []})

In your case it would be smth like this:

def configure_devise_params
  devise_parameter_sanitizer.for(:sign_up) do |u|
    u.permit(:email, :password, :password_confirmation,
    :profile_name, :how_did_you_hear, :first_name, :last_name, :type, {:tag_list => []})
  end
end


来源:https://stackoverflow.com/questions/19880138/in-rails-4-how-can-i-manipulate-strong-parameters-before-creation

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