Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem saving resource using form.fields_for #258

Open
polivers opened this issue Jun 18, 2014 · 1 comment · Fixed by dturnerTS/her#2 · May be fixed by #262
Open

Problem saving resource using form.fields_for #258

polivers opened this issue Jun 18, 2014 · 1 comment · Fixed by dturnerTS/her#2 · May be fixed by #262

Comments

@polivers
Copy link

Hi all,
I have a couple of models, User and Post.

A user has_many :posts, and I've set this up with Her (I can hit /users/1 and /posts/1 to fetch those resources).

In my API, the User model has this:

has_many :posts
accepts_nested_attributes_for :posts

And in my HER app, I have a view like:

<%= form_for @user do |form| %>
  <%= form.text_field :username %>
  <%= form.fields_for :posts do |posts_form| %>
    <%= posts_form.text_field :title %>
  <% end %>
<% end %>

The form renders perfectly, with field names like user[posts_attributes][0][title], however when I try to create a post, Her doesn't send the request to my API and instead returns this error:

no implicit conversion of nil into Hash

It seems to be related to the to_params(attributes, changes={}) call in parse.rb, this line in particular returns nil:

filtered_attributes.merge!(embeded_params(attributes))

For some reason, embedded params returns nil and filtered_attributes.merge! raises the exception.

Anybody else had similar issues? Any help much appreciated!

Rails version 4.1.1, Her 0.7.2

@polivers polivers changed the title Problem saving resource using nested form Problem saving resource using form.fields_for Jun 18, 2014
@polivers
Copy link
Author

Hi all,
I'm not getting very far in solving my issues with Her, unfortunately.

My User model has_many :posts, with accepts_nested_attributes_for :posts, and my form submits fields like:
user[posts_attributes][0][title] which update Her's @user.posts[0].title attribute, all as expected and what I want. and I THEN do @user.save - I'm expecting my api to receive user[posts_attributes].

My form can even nest another level down, Her handle's it and objects in my controller are updated perfectly:

<%= form_for @user do |form| %>
    <%= form.text_field :name %>
    <%= form.fields_for :posts do |posts_form| %>
        <%= posts_form.text_field :title %>

        <%= posts_form.fields_for :tags do |post_tags_form| %>
            <%= post_tags_form.text_field :ids %>
        <% end %>
    <% end %>

    <%= form.submit %>
<% end %>

However, Her seems to just send user[posts] - so the models in my API, which are ActiveRecord models, with their own User has_many :posts and accepts_nested_attributes_for :posts, don't work.

It's also sending the object of the belongs_to association as an attribute of the POST data. For example:

"user" => {
    "posts" => [
        { 
            "name" => "Post name"
            "user"  => "#<User:0x007f0e55011570>"
        }
    ]
}

I'm not sure why "user" => "#<User:0x007f0e55011570>" is there?

Unless I'm doing something obviously wrong, it looks like I'm going to have to give up with using Her and re-write everything as a single Rails app.
Would be very disappointing as it's absolutely great when it works.

thanks for reading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant