In rails 4, how can I manipulate strong parameters

2019-07-18 03:26发布

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条回答
家丑人穷心不美
2楼-- · 2019-07-18 03:50

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
查看更多
登录 后发表回答