Skip to content

Instantly share code, notes, and snippets.

@michaeldhopkins
Created November 1, 2011 03:32
Show Gist options
  • Save michaeldhopkins/1329807 to your computer and use it in GitHub Desktop.
Save michaeldhopkins/1329807 to your computer and use it in GitHub Desktop.
Rails routing not working
Problem: trying to create a new gyro which belongs to restaurant results in
No route matches {:controller=>"gyros", :format=>nil}
Details follow. Most of the code is scaffolded, with some minor tweaks related to nesting gyros inside restaurants.
Restaurants_controller.rb:
def show
@restaurant = Restaurant.find(params[:id])
@gyros = @restaurant.gyros
respond_to do |format|
format.html # show.html.erb
format.json { render json: @restaurant }
end
end
restaurants/show.html.erb:
<p>
<%= link_to "Add a new gyros", new_restaurant_gyro_path(@restaurant) %>
</p>
gyros_controller.rb:
def new
@restaurant = Restaurant.find(params[:restaurant_id])
@gyro = Gyro.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @gyro }
end
end
gyros/new.html.erb:
<h1>New gyro</h1>
<%= render 'form' %>
<%= link_to 'Back', gyros_path %>
routes.rb:
resources :restaurants do
resources :gyros
end
Server log from accessing restaurants/1/gyros/new:
Started GET "/restaurants/1/gyros/new" for 127.0.0.1 at 2011-10-31 22:20:54 -0500
Processing by GyrosController#new as HTML
Parameters: {"restaurant_id"=>"1"}
Restaurant Load (46.8ms) SELECT "restaurants".* FROM "restaurants" WHERE "restaurants"."id" = ? LIMIT 1 [["id", "1"]]
Rendered gyros/_form.html.erb (7.7ms)
Rendered gyros/new.html.erb within layouts/application (8.6ms)
Completed 500 Internal Server Error in 75ms
ActionView::Template::Error (No route matches {:controller=>"gyros", :format=>nil}):
1: <%= form_for(@gyro) do |f| %>
2: <% if @gyro.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@gyro.errors.count, "error") %> prohibited this gyro from being saved:</h2>
app/views/gyros/_form.html.erb:1:in `_app_views_gyros__form_html_erb__4305550367387337329_2162837980'
app/views/gyros/new.html.erb:3:in `_app_views_gyros_new_html_erb__1082692240219481692_2168489420'
app/controllers/gyros_controller.rb:30:in `new'
Rake routes result:
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
restaurant_gyros GET /restaurants/:restaurant_id/gyros(.:format) {:action=>"index", :controller=>"gyros"}
POST /restaurants/:restaurant_id/gyros(.:format) {:action=>"create", :controller=>"gyros"}
new_restaurant_gyro GET /restaurants/:restaurant_id/gyros/new(.:format) {:action=>"new", :controller=>"gyros"}
edit_restaurant_gyro GET /restaurants/:restaurant_id/gyros/:id/edit(.:format) {:action=>"edit", :controller=>"gyros"}
restaurant_gyro GET /restaurants/:restaurant_id/gyros/:id(.:format) {:action=>"show", :controller=>"gyros"}
PUT /restaurants/:restaurant_id/gyros/:id(.:format) {:action=>"update", :controller=>"gyros"}
DELETE /restaurants/:restaurant_id/gyros/:id(.:format) {:action=>"destroy", :controller=>"gyros"}
restaurants GET /restaurants(.:format) {:action=>"index", :controller=>"restaurants"}
POST /restaurants(.:format) {:action=>"create", :controller=>"restaurants"}
new_restaurant GET /restaurants/new(.:format) {:action=>"new", :controller=>"restaurants"}
edit_restaurant GET /restaurants/:id/edit(.:format) {:action=>"edit", :controller=>"restaurants"}
restaurant GET /restaurants/:id(.:format) {:action=>"show", :controller=>"restaurants"}
PUT /restaurants/:id(.:format) {:action=>"update", :controller=>"restaurants"}
DELETE /restaurants/:id(.:format) {:action=>"destroy", :controller=>"restaurants"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment