I have a nested form which is submitting through a create function in my ‘Posts’ controller. One Post has many Locations. My problem is I want to set locations up to ‘Find_Or_Create_by_name’ but not posts.
I figure something like this is a solution:
posts_controller.rb
def create
@post = current_blogger.blog_posts.new(params[:post])
@post.locations = Location.find_or_create_by_name(params[:post])
if @post.save
redirect_to @post, notice: 'Blog post was successfully created.'
else
render action: "new"
end
end
But I get an ‘each’ error.
undefined method `each' for #<Location:0x007fc5c1c2e5c8>
Am I on the right track with this? What’s the next step? Should I be iterating through each ‘location’?
Any help would be great.
EDIT: Here’s the log:
Parameters: {"utf8"=>"✓", authenticity_token"=>"Ffk65s3/aZqVcBvh9S/hOt7zYSighzyt6CSNDIuNt1Q=", "post"=>{"title"=>"This
is about London", "body"=>"This is about London", "tag_list"=>"", "locations_attributes"=>
{"0"=>{"_destroy"=>"false", "name"=>"London", "longitude"=>"-0.1276831", "la
titude"=>"51.5073346"}}}, "_wysihtml5_mode"=>"1", "name"=>"London", "legname"=>"London",
"longitude"=>"-0.1276831", "latitude"=>"51.5073346", "commit"=>"Submit"}
In your Location model
Then in your create action
end