Skip to content
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.

Mass assignment with nested attributes not working #214

Open
pruchai opened this issue Jan 21, 2015 · 3 comments
Open

Mass assignment with nested attributes not working #214

pruchai opened this issue Jan 21, 2015 · 3 comments

Comments

@pruchai
Copy link

pruchai commented Jan 21, 2015

I have no idea if I am doing something wrong or if I am hitting a strange use case that strong_parameters is not even supposed to handle or if it is a strong_parameters issue to begin with.

It does seem like strong_parameters is doing something, because i get a few messages, as expected.

Unpermitted parameters: created_at, updated_at
Unpermitted parameters: created_at, updated_at

But absolutely nothing is happening when I do

contact = Contact.first
contact.update(safe_params)

I enabled mysql query logging to see whats going on, and I don't even see the UPDATE or INSERT query being executed.

I have a detailed description of the issue on StackOverflow, but will copy all information into this issue, if needed and if it has something to do with strong_parameters.
http://stackoverflow.com/questions/28059189/unable-to-update-or-create-records-using-post-with-strong-parameters-and-nested
Thank you.

EDIT: After further investigation it does seem strong_parameters and nested attributes related. If i remove nested attributes from my params everything works. Once i put them back in, it silently fails, so I suspect the problem lies somewhere in the way I permit them?

:host_notification_commands_attributes and :service_notification_commands_attributes get posted as an array of objects.

[
  {
    "id"=>1,
    "command_name"=>"command1",
    "command_line"=>"/usr/local/bin/command1",
    "command_description"=>"Command One",
    "created_at"=>"2015-01-19T17:24:12.000Z",
    "updated_at"=>"2015-01-21T03:29:03.000Z"
 },
 {
    "id"=>2,
    "command_name"=>"command2",
    "command_line"=>"/usr/local/bin/command2",
    "command_description"=>"Command Two",
    "created_at"=>"2015-01-19T17:24:23.000Z",
    "updated_at"=>"2015-01-19T17:24:23.000Z"
    }
]

Am i permitting them correctly?

params.require(:contact)
    .permit(:contact_name, 
        :host_notification_commands_attributes => [ :id, :command_name, :command_line, :command_description ],
        :service_notification_commands_attributes => [:id, :command_name, :command_line, :command_description])
@kaspernj
Copy link

+1

@kaspernj
Copy link

@pruchai: Try this fork and branch:

gem 'strong_parameters', git: "https://github.com/kaspernj/strong_parameters.git", branch: "kj/fixed_integers_as_keys"

Also submitted as PR here:
#221

@thomasfedb
Copy link
Contributor

What version of Rails are you using? If you're using Rails 4 of later, you should submit your issues against rails/rails. This is a compatibility gem for Rails versions prior to 4.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants