Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:22:45+00:00 2026-06-14T17:22:45+00:00

I have a nested form which is submitting through a create function in my

  • 0

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"}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:22:46+00:00Added an answer on June 14, 2026 at 5:22 pm

    In your Location model

    def self.find_or_initialize_location_set(location_set)
      locations = []
      location_set.each do |key, location|
        locations << find_or_initialize_by_name location
      end
      locations
    end
    

    Then in your create action

    def create
      location_set = params[:post].delete(:locations_attributes) unless params[:post][:locations_attributes].blank?
      @post = current_blogger.blog_posts.new(params[:post])
      @post.locations = Location.find_or_initialize_location_set(location_set) unless location_set.nil?
      if @post.save
        redirect_to @post, notice: 'Blog post was successfully created.'
      else
        render action: "new"
      end
    

    end

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested form which creates many locations for each post. Each location
I have a nested form Parent, which accepts attribute for Child. In my controller#new,
I have a Form which contains another form. When I'm submitting the nested form
I have the following form for photo_album which uses the nested forms feature of
I have a nested json. I want to post it as a form input
I have a form on which I use validation. This form has a section
How do I create a nested form for 2 scaffolded models? I have a
I have a nested form in which I would like the form fields to
I've read many posts about doing Rails nested forms, which I think I understand.
I have a nested form which looks something like: <FORM METHOD=GET NAME=mainForm ACTION=<%=response.encodeURL(updateForm.jsp)%>> </FORM>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.