Rails 3.2 Routing Error - ruby-on-rails

In my first approach with Rails I have simply create a void SayController and static hello.rhtml view but when the page http://localhost:3000/say/hello started return me a Routing Error like this:
No route matches [GET] "/say/hello"
Try running rake routes for more information on available routes.
Rails version: 3.2.6

Seems like you didn't add a route for hello to your config/routes.rb file.
YourApp::Application.routes.draw do
match 'say/hello' => 'say#hello', :as => :hello
end
This will match route say/hello to controller say (the part before #) and action hello (the part after #).
:as => :hello makes it a named route so you can refer to it as hello_path from within your app.
The error message tells you to run rake routes (from the console) which will show you the existing routes in your app.

You should have something in your config/routes.rb to define that route. Try:
match 'say/hello' => 'say#hello', :as => 'say_hello'
The go to localhost:3000/say/hello
Also check out this documentation:
http://guides.rubyonrails.org/routing.html

I assume, controller: say and action: hello
Add following to config/route.rb
get 'say/hello' => 'Say#hello'

Related

Error: `Couldn't find Course with 'id'=` when visiting `/courses/show`

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.

Migrating Routes to Rails 4

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

Get "home/index" in Rails project

After running rails generate controller home index in my Rails application, I see this line in routes.rb
get "home/index"
What does this line do? When I removed it, I didn't observe any difference it makes.
see the Rails Routing page for more info but...
It adds, to the routing table, an entry to direct a GET request of the form
http://localhost:3000/home/index
To the HomeController#index action, which will render a response and display the results to the user.
It is a shorthand notation for
match 'home/index' => 'home#index', :via => :get
To see what other routes your application has available, run the following from a terminal while inside your projects directory
rake routes

Custom url in ruby on rails

I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?
You just use a get outside of any resources or namespace block in your routes.rb file:
get 'my_page_here ', :to => 'home#index'
Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.
Always use get, put, post or these variants where possible.
To get a path helper, try:
get 'my_page_here ', :to => 'home#index', :as => :my_page
That way, in your views, my_page_path will equal http://{domain}/my_page_here
you just need to make a routing rule to match that url
in this case it will be something like
match 'my_page_here' => 'your_controller#your_action'
your controller and action will specify the behavior of that page
so you could do
match 'my_page_here' => 'home#index'
or
get 'my_page_here', :to => 'home#index'
as suggested in other responses.
for index action in home controller if you have such a controller
see http://guides.rubyonrails.org/routing.html for more details
also see Ruby on Rails Routes - difference between get and match

Rails routing error despite having route defined?

So I have a route defined like this for my "evisit" controller and "all_messages" action
match "evisits/:token/all_messages" => "evisits#all_messages", :as => :all_evisit_messages
Shows up in rake routes like this:
all_evisit_messages /evisits/:token/all_messages(.:format) {:controller=>"evisits", :action=>"all_messages"}
And I can manually go to it just fine however if I try to redirect to it like so:
redirect_to all_evisit_messages_url(#evisit.token)
I get a "No routes match - Routing Error" as if the route doesn't exist. I think I defined it correctly... anything I'm missing?
Try doing this instead:
redirect_to all_evisit_messages_url(:token => #evisit.token)
Does that work?

Resources