Routing Error, Michael Hartl's Screencasts from railstutorial.org - ruby-on-rails-3

I am following Michael Hartl's Screencasts from railstutorial.org. According to chapter 5, I changed
config/routes.rb to:
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
and _footer.html.erb to:
<%= link_to "About", about_path %>
<%= link_to "Contact", contact_path %>
and _header.html.erb to:
<% logo = image_tag("logo.png", :alt => "Sample App", :class => "round") %>
<%= link_to logo, root_path %>
<nav class="round">
<ul>
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
I am new to Ruby on Rails. I am getting a Routing Error in the browser
No route matches "/pages/contact"
I am using ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux],
rails 3.0.7 and gem 1.6.2
Please help.

Your routes are set up a level. There is no route for /pages/contact, the correct route is "/contact" given your routes file.

For late comers, I just had this same issue. The code from the tutorial shouldn't have an end after those match statements. If you look at the last line of config/routes.rb, you'll see and end there. That's the only end you need.
Instead of:
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
It should be:
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
# All those commented out rules
# All those commented out rules
# All those commented out rules
end

Related

no route matches action in link_to

I am a little confused on how to set the route for a custom action. I have the following link in my view:
<%= link_to 'mark done', finish_task_path(task.id), :method => :post %>
In my tasks_controller I have:
def finish
#task = Task.find(params[:id])
new = {:status => "done"}
#task.update_attributes(new)
redirect_to :action => "index"
end
In my routes file I have:
match '/tasks/:id/finish', :to => 'tasks#finish'
I have also tried the following in my view:
<%= link_to 'mark done', finish_task_path(task.id), :method => :post %>
Which also has not worked. How do I set the route correctly?
You've created a route, but it is not named. Does this work?
match '/tasks/:id/finish', :to => 'tasks#finish', :as => 'finish_task'
Have a look at the output of rake routes to ensure your routes are being declared as you want.

How to change the controller path of a link in rails 3?

How do I get:
<%= link_to 'Back', originalcontrollers_path %>
to be:
<%= link_to 'Back', modifiedcontrollers_path %>
I already have my route updated with:
get "modifiedcontrollers", :to => "originalcontrollers#index"
So the "/modifiedcontroller" url works the same as "/originalcontroller". When creating links I need it to point to the new url. How would I do this?
I am not sure I understand you, but try this:
get "modifiedcontrollers", :to => "originalcontrollers#index", :as => :modifiedcontrollers
get "modifiedcontrollers/new", :to => "originalcontrollers#new", :as => :new_modifiedcontroller
...
so if you need all stack of routes:
resources :modifiedcontrollers, :controller => :originalcontrollers
so now this will work as expected
<%= link_to 'Back', modifiedcontrollers_path %>

Rails Routing Error

Rails is giving me a route error even though the route appears to
be in the route list.
The form is doing a Post to try to hit the update route on the Admin::ProductsController.
The edit route, index route, and show route work fine.
Using Rails 3.0.5 and ruby 1.9.2
Anyone have an idea? I can't seem to see the problem.
Error
No route matches "/admin/products/2039"
Code from ERB File that is generating the form
<%= form_for :product, #product, :url => { :action => "update" } do |f| %>
Products Controller method at this point is just a stub of
def update
puts params.inspect
end
Routes File
Analytics::Application.routes.draw do
match 'login' => 'Authentication#login', :via => [:get, :post]
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
root :to => 'AdminInterface#index', :via => :get
resources :products
resources :publishers, :only => [:edit, :update]
match 'publishers/query/:subset' => 'Publishers#index', :as => :publishers_subset, :via => [:get, :post]
end
end
According to your routes, shouldn't that be
= form_for [:admin, #product] do |f|
Your form_for can just be:
<%= form_for #product do |f| %>
If #product is an existing object then it will automatically know to go the update action of the ProductsController.

Rails 3 Rspec 2 problem with :locale and RESTfull path

I have a problem with edit_user_path(user) in my rspec file:
it "should forward to the requested page after signin" do
user = Factory(:user)
visit edit_user_path(user)
fill_in :email, :with => user.email
fill_in :password, :with => user.password
click_button
response.should render_template('users/edit')
end
I get this error:
Failure/Error: visit edit_user_path(user)
ActionController::RoutingError:
No route matches {:action=>"edit", :controller=>"users", :locale=>#<User id: 1, name: "Max Mustermann", email: "mel#example.com", encrypted_password: "1b0befc5caf12c2abf53d99sdfsd5ba15ebe28362eae21b2fb3...", salt: "331ca07444f5dcee76605bsdfasd95c833b7d5135b36a253723...", created_at: "2011-02-03 18:54:34", updated_at: "2011-02-03 18:54:34">}
I have no clue why it uses the user as :locale and why there is no :id with the user.id. I use :locale to set the language, e.g. localhost:3000/en/home or localhost:3000/de/home.
This is my routs.rb:
Hoapp::Application.routes.draw do
scope '(:locale)', :locale => /de|en/ do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :hotels, :only => [:create, :destroy]
root :to => 'pages#home'
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/about', :to => 'pages#about'
match '/blog', :to => 'pages#blog'
match '/contact', :to => 'pages#contact'
match '/help', :to => 'pages#help'
match '/overview', :to => 'pages#overview'
match '/preview', :to => 'pages#preview'
match '/settings', :to => 'pages#settings'
match 'm', :to => 'mobile#home'
match 'apartments', :to => 'mobile#apartments'
match 'apartment_info', :to => 'mobile#apartment_info'
match 'apartment_images', :to => 'mobile#apartment_images'
match 'apartment_price', :to => 'mobile#apartment_price'
match 'wellness', :to => 'mobile#wellness'
match 'test', :to => 'mobile#test'
scope "m" do
resources :requests, :only => [:new, :create]
match 'request_sent', :to => 'requests#sent'
end
match ':vanity_url/', :to => "mobile#home"
end
Ok I found a way to fix this in the tests (cf. https://github.com/rspec/rspec-rails/issues/255)
I put this code in spec_helper.rb
class ActionView::TestCase
class TestController
def default_url_options
{:locale => 'en'}
end
end
end
Maybe url_for is unable to infer the right parameters when you use a locale? Try it this way:
visit edit_user_path(:id=>user.id)
Including this in spec_helper.rb worked for me:
config.before do
default_url_options[:locale] = I18n.default_locale
end
I tried to put this in config/environments/test.rb and worked for me:
config.action_controller.default_url_options = { locale: I18n.locale }

Rails 3 routing

Ok, I'm relatively new to rails and am working through the book Rails for PHP programmers. To make things interesting I'm using Rails 3 and haml to work through the book, and the book is written using Rails 2.X and erb so some of the examples are outdated.
To the question:
The route example in the book is
map.presentation 'meetings/:meeting_id/presentations/:action/:id',
:controller => "presentations",
:action => "show",
:meeting_id => /\d+/
So that was a pre-Rails 3 route. My adaptation of the above into a Rails 3 route is:
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations#show', :constraints => {:id => /\d/}
My adaptation works for the destroy action, but not for the edit action... and due to my inexperience I don't know what I'm doing wrong. From the article here (http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/) it looks like I'm doing it right, but something isn't right.
The link_to helpers that produce the urls are as follows
= link_to 'edit', :controller => 'presentations',
:action => 'edit',
:meeting_id => presentation.meeting.id,
:id => presentation.id
# Incorrectly Produces: http://localhost:3000/presentations/edit?meeting_id=2&id=1
= link_to 'destroy', { :controller => 'presentations',
:action => 'destroy',
:meeting_id => presentation.meeting.id,
:id => presentation.id },
:confirm => 'Are you sure?',
:method => :delete
# Correctly Produces: http://localhost:3000/meetings/2/presentations/destroy/1
Your help would go a long way to clearing up my routing haziness. Thanks so much!
EDIT
Here's the non-working routes.rb file in its entirety:
UserGroup::Application.routes.draw do
get "presentations/new"
get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
#default route
match ':controller(/:action(/:id(.:format)))'
end
EDIT 2
Thanks to codykrieger I took a look at the rest of the routes file (I know, duh right?). Apparently, all those get "..." routes are added when using the rails generator and help to make some default connections in the app. I commented out the get "presentations/edit" line and, wonder of wonders, my routing adaptation actually does work.
This works:
UserGroup::Application.routes.draw do
get "presentations/new"
#get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
#default route
match ':controller(/:action(/:id(.:format)))'
end
I played around with the order of the routes in the file and also found that if I put my route above all the auto-generated routes, without commenting out the get "presentations/edit" line, my route still has the intended effect.
This also works:
UserGroup::Application.routes.draw do
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
get "presentations/new"
get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
#default route
match ':controller(/:action(/:id(.:format)))'
end
I'm thinking that the latter is the better way to go and I should make my custom routing declarations above the standard generated ones, but I'm not sure. If someone out there wants to comment on which practice is better, or if it's better to entirely remove the generated ones if I want to specify my own, I'd love to hear it.
Thanks all :)
Routing is done on a first match basis (top to bottom) in the routes.rb file.
So you can keep all routes as you have done above if you want, and Rails will use the first one that matches. However, its messy, potentially buggy and totally unnecessary. So the "match..." lines in your code should be enough, and you can remove the "get..." lines.
Furthermore, your "match...presentations...." line has a bug, it should be
...{:id => /\d+/}
else it will validate only 1-digit length ids.
Finally, as far as your overall routes.rb is concerned, you do not need to do
link_to 'edit', :controller => 'presentations',
:action => 'edit',
:meeting_id => presentation.meeting.id,
:id => presentation.id
Instead, you should use edit_meeting_path, new_meeting_path etc. For instance
<%= link_to "New meeting", new_meeting_path %>
<%= link_to "Edit meeting", edit_meeting_path(#meeting) %>
Finally, you should read Rails Routing from the Outside In for best practices on how to design nested routes etc.