No route matches [GET] "/static_pages/home.html.erb" - ruby-on-rails

I am following Michale Hartl's tutorial and I am currently at this step:
Listing 5.23. Adding a mapping for the root route. config/routes.rb
SampleApp::Application.routes.draw do
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end
I have copied his exact coding to my config/routes.rb and continue to get a Routing Error:
Routing Error
No route matches [GET] "/static_pages/home.html".
--
Not sure what to do at this point to get my home routed as the index, and to be able to use root_path to link back to 'Home'.

Remove ../static_pages/.. from the URL you've entered in the browser.

this code is correct
SampleApp::Application.routes.draw do
root :to => 'static_pages#home'
get '/help' => 'static_pages#help'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
end
You do not have to delete the index page, the rspec test in the tutorial is not wrong, this is the updated code with current available methods
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/'
page.should have_content('Sample App')
end
end
This is using ruby -v ruby 2.0.0p47 and rails -v Rails 4.0.3
you can also get a failure if your using before { visit help_path }, using the Rails Console and checking the path app.help_path confirms 'before { visit help_path } is correct but if this code is not in spec/rspec_helpers.rb then the test will fail because it cannot find the path
Named routes should work if you put the following in rspec_helper.rb:
Rspec.configure do |config|
config.include Rails.application.routes.url_helpers
...
end

SampleApp::Application.routes.draw do
root :to => 'static_pages#home'
get '/help' => 'static_pages#help'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
end
this code is right.

You need to delete or rename the public/index.html and then it will work.

Related

Routing error No route matches {:action=>"/", :controller=>"static_pages"}

i'm a ROR newbie who is going through the tutorial by Micheal Hartl. I have follow every steps of the tutorial and i'm stuck with the error regarding the routes. I have gone through all my codes and tried all the solution from google. Long story short i'm trying to get the test pass.
Here is my routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
end`
Here is the error description
1)Error StaticPagesControllerTest#test_should_get_home:
ActionController::UrlGenerationError: No route matches {:action=>"/", :controller=>"static_pages"} test/controllers/static_pages_controller_test.rb:10: in block in class:StaticPagesControllerTest'
2) Error StaticPagesControllerTest#test_should_get_help:
ActionController::UrlGenerationError: No route matches {:action=>"/help", :controller=>"static_pages"}
test/controllers/static_pages_controller_test.rb:16:in `block in class:StaticPagesControllerTest'
Here is how my rake routes looks like
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
I have tried
root 'static_pages#home'
get '/static_pages/help', to: 'static_pages#help'
or
root 'static_pages#home'
match '/help', :to => 'static_pages#help', via: [:get]
yet i still cannot solve the problem. I have been staring at this code and error for days. Please help.
*Edit to include test
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Home | #{#base_title}"
end
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | #{#base_title}"
end

Rails 4 -No route matches, route is defined. Only occurs in testing

So I am following Michael Hartl's Rails tutorial and I am on chapter 5 (https://www.railstutorial.org/book/filling_in_the_layout).
When I run the final test that he adds for the sign up link:
class UsersControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get signup_path
assert_response :success
end
end
I get this error when I run the test:
ERROR["test_should_get_new", UsersControllerTest, 2016-07-01 17:48:25 +0100]
test_should_get_new#UsersControllerTest (1467391705.83s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"/signup", :controller=>"users"}
test/controllers/users_controller_test.rb:6:in `block in <class:UsersControllerTest>'
test/controllers/users_controller_test.rb:6:in `block in <class:UsersControllerTest>'
Here is the routes file:
Rails.application.routes.draw do
root 'static_pages#home'
get '/signup', to: 'users#new'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
end
I have used the signup_path helper in the views as a link and they work fine.
Why is test trying to access an action called "/signup" when I have defined in the routes that "signup" should become the new action in the users controller?
Since it's a controller test (rather than feature test), your controller shouldn't care which url you visited. Therefore, get expects an action, not a path.
Change
get signup_path
to
get :new
If you want to test the route, you can modify your test case to following.
assert_generates asserts that a particular option generate a particular path.
assert_generates '/signup', {controller: 'users', action: 'new'}
assert_recognizes is the reverse of assert_generates. It asserts that a given path is recognized and routes it to a particular spot in your application.
assert_recognizes({ controller: 'users', action: 'new'}, '/signup')
The assert_routing assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of assert_generates and assert_recognizes.
assert_routing({ path: 'signup', method: :get }, { controller: 'users', action: 'new' })

Refactoring Rails Routes to still pass testing

I can't get this to pass:
%w[home help about contact].each do |page|
get :controller => 'static_pages', :action => page
end
I was trying to refactor this code:
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
Please help.
static_pages = %w(home help about contact).map {|p| p.to_sym}
resources :static_pages, only: static_pages do
static_pages.each do |page|
get page, on: :collection
end
end
render
$ rake routes
home_static_pages GET /static_pages/home(.:format) static_pages#home
help_static_pages GET /static_pages/help(.:format) static_pages#help
about_static_pages GET /static_pages/about(.:format) static_pages#about
contact_static_pages GET /static_pages/contact(.:format) static_pages#contact
use only: static_pages to not generate CRUD routes.
I guess if you don't like the original code of:
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
and you want to get the same routes, you could go with a simple loop like:
root to: 'static_pages#home'
%w(help about contact).each do |page|
get "#{page}" => "static_pages##{page}"
end
Though, to me this doesn't really seem like enough of a refactor to justify it.
Edit
To fix the errors on your code, use:
%w(home help about contact).each do |page|
get "/#{page}", :controller => 'static_pages', :action => page
end
Check the api for match for valid arguments to get.

Refactoring 'match' in routes.rb

I've read official guide but still have misunderstanding.
Is this code able for refactoring?
match '/help', :to => 'home#help'
match '/contact', :to => 'home#contact'
match '/about', :to => 'home#about'
help, contact and about are the only actions in the controller home.
I did this on a hunch and it's not mentioned in the documentation, but it looks like it works (I'm on rails 3.1):
controller :home do
get 'help'
get 'contact'
get 'about'
end
This also creates the help_url, help_path, etc helpers.
One warning though, this restricts the http verbs to GET. If you have a POST action (as an example, for a contact form), you could do either:
controller :home do
get 'help'
match 'contact', :via => [:get, :post]
get 'about'
end
or just:
controller :home do
get 'help'
match 'contact'
get 'about'
end
which will allow all http verbs on the contact route. But I find it better to be explicit about the accepted verbs.
You should be able to use rails shorthand here and do:
match 'home/help'
match 'home/contact'
match 'home/about'
Since the method names match this should work.
you could of course do
match '/:action', :controller => :home, :constraints => { :action => /^(help|contact|about)$/ }
but this is neither prettier, nor really shorter

Rails proejct, "Routing Error No route matches "/pages"" Need help

I folllowed this :
http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec:user_signup
.
But when I try to access http://localhost:3000/pages/ it returns "Routing Error No route matches "/pages""
This is my routes.rb
Sample4App::Application.routes.draw do
get "users/new"
match '/signup', :to => 'users#new'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/', :to => 'pages#home'
end
This is my home.html.erb
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
I tried everything I can. But still.
Really need help. Thanks
It seems like your missing the actual route for '/pages/'. Try adding this to your routes.rb
match '/pages' => 'pages#home'
Try this:
Sample4App::Application.routes.draw do
get "users/new"
match 'signup' => 'users#new'
match 'contact' => 'pages#contact'
match 'about' => 'pages#about'
match 'help' => 'pages#help'
match 'pages' => 'pages#home'
root :to => 'pages#index'
end
And make sure you have index action in your Pages controller.
Add this in roots
root :to => 'pages#home'
So, you can access http://localhost:3000
or add
match '/pages', :to => 'pages#home'
so you can access http://localhost:3000/pages
try this
root :to => 'pages#index'
like everyone said earlier but did You deleted index.html.erb from /public/ folder? Delete it and try again - this should resolve the problem :)

Resources