I am just starting with rails and I have a very simple case. I have a "home" controller. There is one action 'index' defined in this controller. When I go to ~/home I get an error msg saying:
uninitialized constant HomesController (I noticed the singular/plural thing).
That's the first thing I don't get (I thought it would automatically go to ~/home/index).
Second thing, if I go to ~/home/edit (note that this action doesn't exist yet), I get also:
uninitialized constant HomesController
But if I go to ~/home/show (show doesn't exist as well) I get a different error message:
No route matches "/home/show"
How can I get 2 differents errors for the same reason (an inexistant action). And What's the deal with this constant?
Thank you
Edit
I'm running rails 3.0
Here is my routes.rb file
Topnotch::Application.routes.draw do
resources :subscriptions
resource :home
get "home/index"
get "subscriptions/index"
root :to => "home#index"
end
You must add the resource "home" to the route.rb.
The controllers are considered to be plural.
If you are new to rails, I suggest you to start using generators - just open a terminal in the project's folder and type in "script/generate scaffold home" (in rails3 it would be "rails g home")
Changes the root route as below:-
root :to => "homes#index".
You must use the plural form in the routes.
Turns out, the routes were correct I was just not using them correctly !
rake routes helped.
Related
I am someone who has always liked sinatra better than rails, and has never had to do a large enough scale project that rails was required (all the sources I have read say that rails is better for larger scale projects) and now I do have to do a large scale project. I have gotten very confused with the url structure of rails. What I am trying to do is the rails equivalent of this:
get "/" do
erb :index
end
get "/home" do
erb :dashboard
end
get "/home/profile" do
erb :profile
end
get "/home/friends" do
erb :friends
end
In the first one I understand that I should put in app/routes.rb I should put root home#index and in the home controller I should put def index end.
In the second one, I understand that I should do the same except replacing index with home.
But for the third and forth ones I have no idea what to do.
Also, is the a RESTful way to do the first two?
You probably want something like this
root 'home#index'
get 'home' => 'home#dashboard'
get 'home/profile' => 'home#profile'
get 'home/friends' => 'home#friends'
remember to use the command rake routes to see all your routes, where they lead and what their names are (if they have any)
I never understood what RESTful means, so someone else will have to answer that part of your question.
K M Rakibul Islam has shown you what can be called a "resourceful" way to do routes (because it uses the keyword resources) but it looks like you're just doing the static pages at this stage, so it's not necessary.
The simplest way to do routes is with this formula:
method url => controller::action, as: route_name
where method can be get, post, patch or delete so you can have different actions linked to the same URL depending on the method the request uses.
Putting a name on the route is optional, but it gives you a clean way to use the route in your views (route_name_path)
When you start making models then you'll find that using the resources keyword comes in handy. Read about it.
You can have this:
resources :home do
collection do
get :profile
end
collection do
get :friends
end
end
end
This will give you routes like this:
profile_home_index GET /home/profile(.:format) home#profile
friends_home_index GET /home/friends(.:format) home#friends
The standard way of declaring the root path:
root 'home#index'
And for the 2nd one, you have to do:
get 'home' => 'home#dashboard'
which will give you this route:
GET /home(.:format) home#dashboard
One route can be defined in many ways that works. But, Rails has conventions that should be followed while defining routes in your Rails app.
I would highly recommend you to take a look at the Rails Routing Guide
I'm in trouble with routes in Rails. I've been trying to get a path like: home/blog/title-article but the closes I get is: home/blog/posts/title-article
I've tried with namespace, scope and one by one with get 'blog' => 'posts#blablabla', but I get errors like UrlGenerationError or NameError every time I change the paths. I've read the official guide and some answers in this forum, but I'm getting more confused hehe
In my last try I generated a controller Blog and a scaffold Post. The output of rake routes is: http://i.stack.imgur.com/gdfPc.png
routes.rb
Rails.application.routes.draw do
scope :blog do
resources :posts
end
get 'blog' => 'blog#index'
root 'pages#index'
...
Thank you!
Now my routes are like: http://i.stack.imgur.com/cKsFG.png
Thank you!
Not sure its what you expect but try:
resources :posts, path: 'blog'
Feels weird though.
Btw, I guess you have the errors due to url helpers.
use rake routes to check the existing routes and the related url helpers.
Doc lives here
When I click this link on a page <%= link_to 'Submit an Application', new_s_n_d_sub_path %> it is giving me the following error:
Routing Error uninitialized constant SNDSubsController
This is a simple thing, but I'm not sure where I'm messing up.
I have the s_n_d_subs_controller.rb file with:
class SNDSubsController < ApplicationController
def new
...
end
Within views I have the file: s_n_d_subs/new.html.erb
Within routes I have resources :s_n_d_subs
Rake Routes:
s_n_d_subs GET /s_n_d_subs(.:format) s_n_d_subs#index
POST /s_n_d_subs(.:format) s_n_d_subs#create
new_s_n_d_sub GET /s_n_d_subs/new(.:format) s_n_d_subs#new
edit_s_n_d_sub GET /s_n_d_subs/:id/edit(.:format) s_n_d_subs#edit
s_n_d_sub GET /s_n_d_subs/:id(.:format) s_n_d_subs#show
PATCH /s_n_d_subs/:id(.:format) s_n_d_subs#update
PUT /s_n_d_subs/:id(.:format) s_n_d_subs#update
DELETE /s_n_d_subs/:id(.:format) s_n_d_subs#destroy
root GET / welcome#index
What am I missing?
I ended up just changing the names of my models and regenerating the controllers and views. I think what was throwing me off was I named my models with single letters representing words: s_n_d_subs which was causing me confusion when I generated controllers and views. So what I did was changed my model names so each part separated by an underline had at least two letters: ex: surv_dev.rb. This way when I generated controllers and routes it all worked just fine.
Your SNDSubs Is treated as a constant rather than a class. :(
Try this :
#app/controllers/sn_d_subs_controller.rb
class SnDSubsController
def new
end
end
In your routes:
resources :sn_d_subs
I am doing the very intro course in rails, and so far have completed the following
Made a new rails app: rails new shovell ("shovell" is the name of the app)
Installed all the gems and what not: bundle install
Generated a model rails generate model Story name link
Generated a controller rails generate controller Stories index
And now when I point to http://localhost:3000/stories, I get a error that says "Routing Error
No route matches [GET] "/stories" "
And the following is my routes.rb:
Shovell::Application.routes.draw do
get "stories/index"
# a bunch of comments
end
So I don't know what Im doing wrong, and why its not showing up the default welcome message, but rather giving me an error. Thanks for your help!
However, if you do:
http://localhost:3000/stories/index
you may get the page, although that's not the rails way.
First thing, read and understand the rails routing guide
Then in order to fix your code, on the routing you can write
Shovell::Aplication.routes.draw do
resources :stories
end
Or if you want custom routes instead of a rest resource
Shovel::Application.routes.draw do
match "stores", to: "my_controller#my_action"
end
And you can also name the custom route
Shovel::Application.routes.draw do
match "stores", to: "my_controller#my_action", as: :store_index
end
So with a name you can use the route name on your rails app
link_to("Store Index", store_index_path)
You've defined a route to /stories/index, but you've not defined one to /stories, which is why this is failing.
You should define this route like this:
get '/stories', :to => "stories#index"
For more information, see The Routing Guide
In my rails 3.2.2 app I have the following in my routes.rb:
scope "abc" do
get "hello/index"
end
Which should link "/abc/hello/index" to my index-action in my hello-controller, right?
Instead, I get the error "uninitialized constant Abc"
If I change it to the following
scope "abc" do
match "hello/index", to: "hello#index", via: :get
end
it works just fine.
From my understanding of the routing engine, the two should be the same, shouldn't they?
(See e.g.: http://guides.rubyonrails.org/routing.html#http-verb-constraints )
Also, if you do a "rails g controller hello index" a route named
get "hello/index"
is autocreated suggesting that this is the standard way of doing a non-restful get route.
So why can't I scope such a route? Any ideas?
The examples are using the notation scope "/abc", maybe the initial / is required.