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.
Related
I'm new to Rails, and I'm setting up models/controllers for Course and some other models.
When I visit the /courses/show URL in my browser I get the following error:
Couldn't find Course with 'id'=
Screenshot here.
Here's the relevant line from my rake routes and routes.rb:
rake routes
courses_show GET /courses/show(.:format) courses#show
config/routes.rb
get 'courses/show'
You have specified the four routes without any :id parameter, I don't know why you would expect them to have an :id parameter.
I'd recommend that you read the Rails guide on routing and also read the comments in the generated config/routes.rb, in that file you'll see comments like this:
# Example of regular route:
# get 'products/:id' => 'catalog#view'
So, extrapolating that to your example you might end up with:
get 'courses/:id' => 'courses#show'
The example that follows that one shows how to add a named route helper using the :as option:
get 'courses/:id' => 'courses#show', as: :courses_show
Something you'll also see when you read the guide or the comments is that you can use the resources helper to create standard restful routes.
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 am about to migrate my rails 3 application to rails 4.
There are some additional routes on my ressources that make some problems.
I get an error message for this lines in my route file:
resources :teams do
...
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :apply_membership
...
This is the generated error message
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
In rails3 it was possible to define a get and a post route using the same alias and routing them to different controler methods.
Can I do this in rails4, too?
And if yes, how does it have to look like in my route file?
You can not take two route name with same name. but you have done it. so please change,
get 'apply' => 'teams#apply_membership', as: :apply_membership
post 'apply' => 'teams#apply_membership_save', as: :update_membership
Take a look here for rails routings. http://guides.rubyonrails.org/routing.html
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
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.