No route matches with Nested Resources

2019-04-13 18:13发布

问题:

I have two associated tables. Venues and Specials. A venue can have many specials. Once a user has created a venue I wish to allow them to create a special on the venues#index page. By using nested resources I have achieved the desired URL: /venues/5/specials/new.

However, my current code results with: No route matches {:controller=>"specials", :format=>nil}

I'm guessing the error is with my SpecialsController and the def new and def create functions. I would like the URL to take me to a form page where I can enter new data for the specials

<%= link_to 'Add Special', new_venue_special_path(venue) %>



App1::Application.routes.draw do

  resources :venues do 
    resources :specials
end


def new
    @venue = Venue.find(params[:venue_id])
      @special = @venue.specials.build
        respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @special }
       end
      end


  def create
    @venue = Venue.find(params[:venue_id])
    @special = @venue.specials.build(params[:special])


    respond_to do |format|
      if @special.save
        format.html { redirect_to @special, notice: 'Special was successfully created.' }
        format.json { render json: @special, status: :created, location: @special }
      else
        format.html { render action: "new" }
        format.json { render json: @special.errors, status: :unprocessable_entity }
      end
    end
  end

Backtrace

Started GET "/venues/4/specials/new" for 127.0.0.1 at 2011-12-06 23:36:01 +0200
  Processing by SpecialsController#new as HTML
  Parameters: {"venue_id"=>"4"}
  [1m[36mVenue Load (0.2ms)[0m  [1mSELECT "venues".* FROM "venues" WHERE "venues"."id" = $1 LIMIT 1[0m  [["id", "4"]]
Rendered specials/_form.html.erb (1.9ms)
Rendered specials/new.html.erb within layouts/application (2.6ms)
Completed 500 Internal Server Error in 97ms

ActionView::Template::Error (No route matches {:controller=>"specials", :format=>nil}):
    1: <%= form_for(@special) do |f| %>
    2:   <% if @special.errors.any? %>
    3:     <div id="error_explanation">
    4:       <h2><%= pluralize(@special.errors.count, "error") %> prohibited this special from being saved:</h2>
  app/views/specials/_form.html.erb:1:in `_app_views_specials__form_html_erb__2784079234875518470_70162904892440'
  app/views/specials/new.html.erb:7:in `_app_views_specials_new_html_erb__115378566176177893_70162906293160'
  app/controllers/specials_controller.rb:30:in `new'

Rendered /Users/andrewlynch/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)

回答1:

redirect_to @special

this will default to "specials_path", but you're using venue_special_path

you probably want:

redirect_to [@venue, @special]

and in the form you will need the same:

<%= form_for([@venue, @special]) do |f| %>

basically - the issue is that you have a nested resource... which means that every place where you are declaring a url path (including implicit places like form_for) has to be replaced with both the @venue and the @special, instead of just the @special.

you may come across this same "bug" elsewhere in your generated scaffold code... just do the same thing and you should be good.