simple form - Rails SimpleForm with nested models: submission redirects to parent model -


i have user has_many :recipes. i'm using simple_form, , trying use form create new recipe. simple, right? i've looked through dozen questions, , thought had fixed gotchas, including accepts_nested_attributes_for , everything.

now, when submit form create new recipe, redirects me user edit form errors user. here's relevant code.

class user < activerecord::base   ...   attr_accessible ..., :recipes_attributes    has_many :recipes   accepts_nested_attributes_for :recipes end   class recipe < activerecord::base   ...   belongs_to :user  end 

and recipes/new.html.haml

= simple_nested_form_for @user |f|    = f.simple_fields_for :recipes, @user.recipes |rf|     = rf.input :name     = rf.input :source     = rf.input :link     = rf.input :season, collection: %w(spring summer fall winter), prompt: 'choose season', required: false     = rf.input :protein, as: :radio_buttons, required: false     = rf.input :course, collection: ['appeteizer', 'soup', 'dessert', 'entrée', 'side', 'salad'], prompt: 'choose course', required: false     = rf.input :featured, as: :boolean, required: false     = rf.input :directions, as: :text     .actions       = f.submit 'save'       or       = link_to 'cancel', user_recipes_path(@user) 

i tried = f.simple_fields_for :recipe |rf|

and here's new method of recipescontroller

  def new     @user = user.find(params[:user_id])     @recipe = @user.recipes.build || @recipe.new      respond_to |format|       format.html       format.json { render json: @recipe }     end   end 

it looks it's trying post users controller instead of recipes controller, explains why redirects me editing user. here's log of put request:

started put "/users/4" 127.0.0.1 @ 2013-05-19 22:31:07 -0700 processing userscontroller#update html   parameters: {"utf8"=>"✓", "authenticity_token"=>"qmco025nqr9vjt49to1gq7/edv4mslvuwhyoteihi2e=", "user"=>{"recipes_attributes"=>{"0"=>{"name"=>"dffd", "source"=>"dfswer", "link"=>"ewrre", "season"=>"", "course"=>"", "featured"=>"0", "directions"=>"werew"}}}, "commit"=>"save", "id"=>"4"} 

i know technically form_for @user, since it's creating recipe, how make put go recipes create new recipe? , want put recipes_attributes, or recipe_attributes? it's new recipe, , tried getting accept nested attributes recipe or recipes (i made forms match accordingly). guess i'm confused that, too.

you have created form user simple_nested_form_for @user . thats why form gets submitted users_controller.

if dont have user related fields in form, create form recipes

form_for([@user, @recipe]) 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -