I have defined three routes in routes.rb
Rails.application.routes.draw do
root to: 'pages#lottery'
get 'pages/about'
get 'pages/contact'
get 'pages/lottery'
end
And when I run "rake routes" in my command line, I get the following:
Prefix Verb URI Pattern Controller#Action
root GET / pages#lottery
pages_about GET /pages/about(.:format) pages#about
pages_contact GET /pages/contact(.:format) pages#contact
pages_lottery GET /pages/lottery(.:format) pages#lottery
But when I got to the localhost:3000/pages/contact I get the error:
"No route matches [GET] "/pages/contact.html.erb"
And also "You don't have any routes defined!"
Does anybody know the problem?
Ugh rookie mistake. I had two tabs open in my command line. My rails s pages was in a different directory. Thanks for the help everyone, I appreciate it.
You should not be going to the URL /pages/contact.html.erb, it should be just /pages/contact.html.
Rails provides nice helpers to make it easy to get the right path, for example, pages_contact_path (from your rake routes).
Your pages_controller.rb should consist of three methods based on your routes listed.
pages_controller.rb
class PagesController < ApplicationController
def lottery
end
def about
end
def contact
end
end
views/pages/contact.html.erb
<p>Hello World</p>
Start up your rails server - rails s and navigate to localhost:3000/pages/contact and you should see Hello World
Related
I'm new to Rails. I've got a routing error bellow when I try to access http://localhost:3000/index.
No route matches [GET] "/index"
Rails.root: /Users/[...]/taskleaf
Routes
Helper HTTP Verb Path Controller#Action
root_path GET / tasks#index
tasks_path GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
[...]
Here is the content of my route.rb file:
Rails.application.routes.draw do
root to: 'tasks#index'
resources :tasks
end
I would be very grateful if you could help me out ...
from what you've provided, try changing it to either '/' or '/tasks/index' then you may get what you're looking for.
Or, add this code in your routes.rb file get '/index', to: 'tasks#index', which would direct /index path to the index action in your tasks controller.
Here is how Rails routing works: when you request '/index', it'll try to find #index route in your routes.rb file, which doesn't exist since the index action lies within your 'tasks' controller (from what I've seen from your codes). In the code, you defined the root page ('/') to be '/tasks/index' (try visiting '/' and '/tasks/index', they should be the same).
Remember, whenever you've seen a routing error, it is likely that you didn't define the route in routes.rb file
Feel free to give it a try and update here if there's any further errors/problems.
Cheers :)
Haven't been able to find anything specific to this issue (other searches deal with forms and such). It's probably a simple oversight on my part. But what on earth am I missing?
GOAL: I'm simply trying to redirect from the /login page URL to the /dashboard URL if a session exists.
EXPECTED OUTCOME: Calling redirect_to dashboard_index_url or redirect_to '/dashboard' should go to https://mydomain/dashboard
CURRENT OUTCOME: if I go to https://mydomain after creating a session it redirects me to https://mydomaindashboard, note the missing slash
ATTEMPTED SOLUTIONS:
Manually type the URL https://mydomain/dashboard after creating a session, RESULT: works, so the proper route seems to exist
Make manual route in routes.rb, RESULT: behavior is exactly the same as resource routing with the missing slash
Clear browser cache, use different browswers RESULT: all exhibit same behavior
Here's what I have (abbreviated to relevant parts):
class LoginController < ApplicationController
def index
redirect_to dashboard_index_url if session[:user_id]
end
#...
end
class DashboardController < ApplicationController
before_action :require_login # calls redirect_to root_url unless session[:user_id]
def index
#...
end
end
# In routes.rb:
resources :login
resources :dashboard
# have also tried things like (removed the above line for these)
get 'dashboard' => "dashboard#index"
#Ryan Here is the current output for the routes:
$ rake routes
Prefix Verb URI Pattern Controller#Action
login_index GET /login(.:format) login#index
POST /login(.:format) login#create
new_login GET /login/new(.:format) login#new
edit_login GET /login/:id/edit(.:format) login#edit
login GET /login/:id(.:format) login#show
PATCH /login/:id(.:format) login#update
PUT /login/:id(.:format) login#update
DELETE /login/:id(.:format) login#destroy
dashboard GET /dashboard(.:format) dashboard#index
dashboard_index GET /dashboard(.:format) dashboard#index
POST /dashboard(.:format) dashboard#create
new_dashboard GET /dashboard/new(.:format) dashboard#new
edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit
GET /dashboard/:id(.:format) dashboard#show
PATCH /dashboard/:id(.:format) dashboard#update
PUT /dashboard/:id(.:format) dashboard#update
DELETE /dashboard/:id(.:format) dashboard#destroy
root GET / login#index
SOLVED:
Well, I found the problem and it was not actually Rails, it was Apache-Passenger. There was a config with a redirect (so that all HTTP get redirected to HTTPS) at the apache level that didn't have the trailing slash floating around and causing trouble. (smacks forehead)
Gotta love those red herrings. Thanks so much guys for the quick help!
for use dashboard_index_url, you have to write it in your routes file. However as you've created in routes.rb a dashboard resource, a dashboards_url is available to you and it leads to /dashboards/index
Other way to nail this task is create a mapping
get 'dashboard' => "dashboard#index" as: :dashboard_index
and dashboard_index_url and dashboard_index_path will be available for you
Update 1
Please, look on this SO question
the problem is that path give you relative route and url - absolute. that's why you don't have additional slash in your path. Try path instead of url and it should work.
Update 2
try dashboard_index_path
How about changing your routes to
get '/login', to: redirect('/dashboard')
You can even tailor the resulting link to something more specific to your user by
get '/login', to: redirect('/dashboard'), as: '/dashboard/user[:username]' if you need to.
On a side-note, you should try cleaning up your routes as you go. There are a lot there that you are not using. Change it to
resources :dashboard, only: [:index] and add to it as you need. E.g only: [:index, :show] etc. If another developer was needed to modify your app one day we'd look at your routes and perhaps make incorrect assumptions about what we can do.
Justin
I am following the tutorial Ruby on Rails Tutorials by Michael Hartl and try to finish the last step in section 3.4.4. When I change routes.rb to
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end
and relaunch http://localhost:3000/static_pages , they said "We're sorry, but something went wrong". Then I relaunch http://localhost:3000/static_pages/help and it works. Then I check the routes by
rake routes
And the result shows:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
static_pages_about GET /static_pages/about(.:format) static_pages#about
I check the content in file static_pages_controller.rb and no difference as that in tutorial. Could someone tell me what is wrong?
Well, with this route mapping root 'static_pages#home', you said, when you hit http://localhost:3000/ url, you will be mapped to the home action of the StaticPages controller.
But you didn't define any mapping for http://localhost:3000/static_pages, it is incorrect url as per the route.rb file. That's why you got the error.
Read the first line of the output of rake routes, it is clearly telling what you have defined.
The line root 'static_pages#home' is equivalent to get '/', to: 'static_pages#home'. This means that the URL localhost:3000/ is being routed to the #home action on the StaticPages controller.
If you want localhost:3000/static_pages to point to the #home action, add this line:
get 'static_pages', to: 'static_pages#home'
In routes.rb file: root "pages#index"
pages_controller.rb file
class PagesController < ApplicationController
def index
render 'home'
end
def about
end
end
view named home.html.haml
rake routes returns
Prefix Verb URI Pattern Controller#Action
home GET /home(.:format) pages#home
about GET /about(.:format) pages#about
localhost just says "We're sorry, but something went wrong.", the console says ActionController::RoutingError (No route matches [GET] "/home"):
On Rails 4.2.0.beta2
*Edit for Rails version and console error
Indeed, rake routes says that a GET /home will match pages#home, the home action in pages controller.
Can you show us more of your routes.rb file ?
Moreover, why don't you rename your home.html.haml file to index.html.haml file, thus your index action can become
def index
end
If you don't specify which view to render, Rails will try to find a view with the name of your action.
I would update your routes.rb file to:
root 'pages#index'
get '/home', to: 'pages#index'
get '/about', to: 'pages#about'
As suggested by Benjamin, I do think that it would be a better design to rename your home.html.erb to index.html.erb. This way you can remove the render 'home' from your index method.
What is odd is that your rake routes output says that /home will be redirected to pages#home, which is a controller method that you don't have.
longtime reader & first time poster, so I'd appreciate it if you went easy on me.
Recently started teaching myself RoR, and have been hacking away at a personal project/website to get the hang of things. Here's my problem:
I'm using the Simple Navigation gem to generate links. Inside navigation.rb I'm trying to call:
primary.item :home, 'Home', home_path
...where home is a view and controller that displays my front page:
home > index.html.erb (just contains a bunch of standard HTML, but let me know if it'd be useful to include)
and controllers > home_controller.rb:
class HomeController < ApplicationController
def index
#posts = Post.all
end
end
I'm getting this error, though:
Routing Error
No route matches {:action=>"show", :controller=>"home"}
Try running rake routes for more information on available routes.
... so I run rake routes, and can definitely see "home#show" in there.
My routes.rb file, as well, has this in it:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index"
resources :posts
resources :home
So I'm a little baffled, and I'm sure it's because of my inexperience or general inability to understand what I'm doing, but I'd really appreciate some help as it's more or less a road block that I haven't been able to overcome.
Appreciate it!
Jay
It is because of the resources; if you are not using the resources remove resources :home
This could be your routes:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index", :as => "home"
resources :posts
See how I removed the resources :home. In home/index the :as represents an alias, so you can use the alias as a method, adding "path" at the end of the name.
Check this guide about routes and resources: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default