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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:55:57+00:00 2026-05-17T18:55:57+00:00

I think I am thinking about routing all wrong. I have a very simple,

  • 0

I think I am thinking about routing all wrong. I have a very simple, two model set-up: Product and Photo. Product has_many :photos, and Photo belongs_to :product.

Product has a full scaffold while Photo has a photos_controller that I am working on.

In routes.rb we have:
resources :products (generated by the scaffold)

As photos are a nested resource of a product I changed this to:

resources :products do
    resources :photos
  end

and finally:

root :to => "products#index"

Happily rake routes spits out:

  products GET             {:controller=>"products", :action=>"index"}
  products POST            {:controller=>"products", :action=>"create"}
  new_product GET          {:controller=>"products", :action=>"new"}
  edit_product GET         {:controller=>"products", :action=>"edit"}
  product GET              {:controller=>"products", :action=>"show"}
  product PUT              {:controller=>"products", :action=>"update"}
  product DELETE           {:controller=>"products", :action=>"destroy"}
  product_photos GET       {:controller=>"photos", :action=>"index"}
  product_photos POST      {:controller=>"photos", :action=>"create"}
  new_product_photo GET    {:controller=>"photos", :action=>"new"}
  edit_product_photo GET   {:controller=>"photos", :action=>"edit"}
  product_photo GET        {:controller=>"photos", :action=>"show"}
  product_photo PUT        {:controller=>"photos", :action=>"update"}
  product_photo DELETE     {:controller=>"photos", :action=>"destroy"}
  products GET             {:controller=>"products", :action=>"index"}
  products POST            {:controller=>"products", :action=>"create"}
  new_product GET          {:controller=>"products", :action=>"new"}
  edit_product GET         {:controller=>"products", :action=>"edit"}
  product GET              {:controller=>"products", :action=>"show"}
  product PUT              {:controller=>"products", :action=>"update"}
  product DELETE           {:controller=>"products", :action=>"destroy"}
  root                     {:controller=>"products", :action=>"index"}

which means that the form in products/new will POST to products#create which I want to then redirect to photos#new and have a form for uploading the product_photos generated by the corresponding photos/new.html.erb which will POST to photos#create, right?

in product_controller.rb:

def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        redirect_to new_product_photo_path, :notice => 'Product was successfully created.'
      else
        render :action => "new"
      end
    end
  end

and in photos_controller.rb (for now):

def new
    @photo = Photo.new
  end

So why oh why oh why do I get:

Routing Error

No route matches {:controller=>"photos", :action=>"new"}

when rake routes clearly says I do, I have a photos_controller, a new action in the photos_controller, and new_product_photo_path is clearly asking to go the right way? (I also have a photos/new.html.erb that has a simple <h1>Photos</h1> for something to render).

I can only conclude that I am thinking about this all the wrong way, or that I have made an error in Convention over Configuration that I can’t see.

Anybody?

Thanks and Kind Regards,
Adam

  • 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-05-17T18:55:58+00:00Added an answer on May 17, 2026 at 6:55 pm

    Updated answer:

    Using a nested resource means (in this case) that you can only create a new photo in the context of a product. This means that the application has to know which product the photo to be created belongs to.

    In the case of your redirect this means you have to add the product object as a parameter to new_product_photo_path:

    redirect_to new_product_photo_path(@product)
    

    Original answer:

    This is because you made it a nested resource. /products/1/photos/new probably does work. If you want to be able to create new photos via /photos/new too, you have to add an ‘unnested’ resource too.

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

Sidebar

Related Questions

What do you think about this build tool ? I'm thinking of migrating from
I'm curious How do you people think ? (I mean a way of thinking)
I'm thinking about defining Restful urls for a new project (developed with Ruby On
In my application I have two threads a main thread which is busy most
I think the file that is produced is an .asm file, any idea how
I think most people know how to do this via the GUI (right click
I think most people here understand the importance of fully automated builds. The problem
I think this is specific to IE 6.0 but... In JavaScript I add a
I think it can be done by applying the transformation matrix of the scenegraph
I think that java executables (jar files) are trivial to decompile and get the

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.