Rails 4 Unpermitted Parameters for Array

前端 未结 5 766
忘掉有多难
忘掉有多难 2020-12-07 22:07

I have an array field in my model and I\'m attempting to update it.

My strong parameter method is below

def post_params
  params[\"post\"][\"categor         


        
相关标签:
5条回答
  • 2020-12-07 22:43

    Try this

    params.require(:post).permit(:name, :email, :categories => [])
    

    (Disregard my comment, I don't think that matters)

    0 讨论(0)
  • 2020-12-07 22:44

    I had the same problem but in my case I had also to change from:

    <input type="checkbox" name="photographer[attending]" value="Baku">

    to:

    <input type="checkbox" name="photographer[attending][]" value="Baku">

    Hope this is helping someone.

    0 讨论(0)
  • 2020-12-07 22:50

    I had the same problem, but simply adding array to permit was not enough. I had to add type, too. This way:

    params.require(:transaction).permit(:name, :tag_ids => [:id])
    

    I am not sure if this is perfect solution, but after that, the 'Unpermitted parameters' log disappeared.

    I found hint for that solution from this excellent post: http://patshaughnessy.net/2014/6/16/a-rule-of-thumb-for-strong-parameters

    0 讨论(0)
  • 2020-12-07 22:54

    in rails 4, that would be,

    params.require(:post).permit(:name, :email, {:categories => []})
    
    0 讨论(0)
  • 2020-12-07 23:01

    The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO, ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile.

    To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

    params.permit(:id => [])
    

    This is what the strong parameters documentation on Github says:

    params.require(:post).permit(:name, :email, :categories => [])
    

    I hope this works out for you.

    0 讨论(0)
提交回复
热议问题