In rails 4, how can I manipulate strong parameters

2019-07-18 03:44发布

问题:

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