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 8901495
In Process

The Archive Base Latest Questions

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

I’ve read over 20 StackOverflow pieces (and plenty others across the web) on very

  • 0

I’ve read over 20 StackOverflow pieces (and plenty others across the web) on very similar issues and tried their solutions, but none of it has worked. Please help this beginner!

In particular, the solution I’ve found most often is

form_for [@parent, @child] do |f|

but it doesn’t fix the error the way it did for other people.

Error:

The error occurs at localhost:3000/locations/1/restaurants/new

NoMethodError in Restaurants#new

Showing /app/views/restaurants/_form.html.erb where line #1 raised:

undefined method `restaurants_path' for #<#<Class:0x007fb7ab89ea80>:0x007fb7aaadc3c0>

Extracted source (around line #1):

1: <%= form_for [@location, @restaurant] do |f| %>
2:   <% if @restaurant.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@restaurant.errors.count, "error") %> prohibited this restaurant from being saved:</h2> 

I can’t find any mention of restaurants_path in any of the app code, so I’m assuming it’s some magical Rails default.

Code:

I’m using has_many/belongs_to models: A location has many restaurants.

config/routes.rb

  resources :locations do
    resources :restaurants
  end

$ rake routes

location_restaurants GET    /locations/:location_id/restaurants(.:format)          restaurants#index
                         POST   /locations/:location_id/restaurants(.:format)          restaurants#create
new_location_restaurant GET    /locations/:location_id/restaurants/new(.:format)      restaurants#new
edit_location_restaurant GET    /locations/:location_id/restaurants/:id/edit(.:format) restaurants#edit
location_restaurant GET    /locations/:location_id/restaurants/:id(.:format)      restaurants#show
                         PUT    /locations/:location_id/restaurants/:id(.:format)      restaurants#update
                         DELETE /locations/:location_id/restaurants/:id(.:format)      restaurants#destroy

app/controllers/restaurants_controller.rb

  def new
    @restaurant = Restaurant.new

    respond_to do |format|
      format.html
      format.json { render json: @restaurant }
    end
  end

  def edit
    @restaurant = Restaurant.find(params[:id])
  end

  def create
    @location = Location.find(params[:location_id])
    @restaurant = @location.restaurants.create(params[:restaurant])

    respond_to do |format|
      if @restaurant.save
    format.html { redirect_to location_restaurants_path(@location), notice: 'Restaurant was successfully created.' }
        format.json { render json: @restaurant, status: :created, location: @restaurant }
      else
        format.html { render action: "new" }
        format.json { render json: @restaurant.errors, status: :unprocessable_entity }
      end
    end
  end
  • 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-15T01:22:15+00:00Added an answer on June 15, 2026 at 1:22 am

    You have not initialized a @location, which is required for your form (in the line form_for [@location, @restaurant]). Just add a line that looks it up (using Location.find) to your new action and then build @restaurant so that it is associated with the location:

    def new
      @location = Location.find(params[:location_id])
      @restaurant = @location.restaurants.build
      ...
    
    end
    

    Since you’ll need @location for all your actions (including edit, which I assume must not be working either in your current code), it would make sense to put it into a separate method and then call it from a before_filter, i.e.:

    before_filter :find_location
    
    private
    
    def find_location
      @location = Location.find(params[:location_id])
    end
    

    Then you can also remove the line where you find the location in your create action (and from the new action).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.