How to display an action when specifying controller url - ruby-on-rails

I was wondering how to display an action when specifying a controller url. For example; say I have DemoController.rb, and views/demo/index.html.erb file. Also I've specified the route in routes.rb
get 'demo/index'
How can I setup the project so that when I type "localhost:3000/demo" it renders the same layout as "localhost:3000/demo/index"?
Currently when I type in "localhost:3000/demo" I get a "no route matches" error.

You need a get 'demo' => 'demo#index' instead for that URL to work.
That said, you might want to use Rails conventions and maybe pluralize the controller and use RESTful routes instead:
resources :demos, only: :index

Related

Ruby on Rails optional route segments

I'm trying to create two routes in my Rails application with optional parameters.
Here's my routes file:
get '(a)(/:area_id/)l/:location_id/(*url_title)', to: 'locations#show', as: :location
get 'a/:area_id/(*url_title)', to: 'areas#show', as: :area
Navigating to the following URLs correctly routes me to the right controller:
http://localhost:3000/a/1/l/2/seo-friendly-title.html
http://localhost:3000/a/1/seo-friendly-title.html
However, navigating to this url does not work:
http://localhost:3000/l/2/seo-friendly-title.html
I receive a No route matches error. Is it possible to make the a/:area_id portion optional when the l/:location_id portion is present?
Using Rails 4.2.4.
You could just add another route...
get 'l/:location_id/(*url_title)', to: 'locations#show'
There's no reason why two routes can't map to the same action.

Cant create a link to named routes

I want to create a link to a named route
My routes.db have the following rule
match '/tablero', to: 'tablero#index',via: 'get' , as: 'tablero_main'
I can see the route using rake routes
tablero_main GET /tablero(.:format) tablero#index
But when i use the link_to as follows i get the "undefined local variable or method `tablero_main'" error.
<%= link_to "Tablero",tablero_main %>
Is there anything else i am missing?
You need to append path to the method name, like so:
<%= link_to "Tablero", tablero_main_path %>
Routes
To help you further, you'll need to also consider the role of resources in your routes
As Rails uses a resourceful routing infrastructure, every route you create should be based around a resource. In your case:
#config/routes.rb
resources :tablero, only: :index #-> domain.com/tablero
Admittedly, this will give you the path tablero_index_path, rather than tablero_main_path, but it ensures your routes are not only DRY, but also extensible. Nothing worse than having 100's of "match routes in a route file.
--
Helpers
After that, remember to use the correct route_path helper:
Each "route" path is basically just a helper method (which builds a URL for you). When using link_to, you need to reference the path helper directly. You didn't do this, which lead Rails to come back with the undefined method error

Make collection route generic

I'm working with Rails 3.2 in a project, and I need to create a route for all controllers.
For now, the route is:
resources :people do
collection do
get 'search_for'
end
end
I need thsi "search_for" action for all controllers in application.
Thanks.
you can use this route
match "/:controller/search_for" => redirect("/%{controller}/search_for")
UPDATE: the route above will not work and will cause a redirect loop error simply because it causes a 301 redirect to the same route. solution is to just use
match "/:controller/search_for"
be sure to place this route above all routes so routes that go to the show action will not override this route like.
Simply put this above all routes as:
get ':controller/search_for'

Routing custom action in Rails 3

I have a very simple question.
Trying to figure out what is the simplest way to route the custom action in rails 3.
Let's say i have controller UsersController and action promote_to_premium
Nor
http://localhost:3000/users/#{user_id}/promote_to_premium
neither
http://localhost:3000/users/promote_to_premium/#{user_id}
works.
Should I specify in routes.rb every custom action that differs from new/delete/update/create/ect/....?????
Thank You.
Yes you need to specify in your routes.rb.
Example:
resources :users do
member do
post :promote_to_premium
end
end
This way you can access the route like this:
http://localhost:3000/users/#{user_id}/promote_to_premium
You should use this in routes.rb:
match "/users/:id/promote_to_premium" => "users#promote_to_premium"
You should mention the route in routes.rb file for custom methods in the controller.
You can specify the routes using either get"" or a match""=>"" or a "post"
when you write get "controller/something" something should be an action(method) called by the name "something" in your controller. But in your case you cannot use get"controller/:id" as there is no ":id" method in your controller. So, you should match your controller/:id to some 'action' in your controller.
Hence you need to write
"match users/:id/promote_to_premium"=> "users#promote_to_premium"
But if you are writing something into the database then you should use 'post'. From whatever i know, i think you can try
match 'users/:id/promote_to_premium' => 'users#promote_to_premium', :via => :post
You can study more about routes in the below link:
http://guides.rubyonrails.org/routing.html
Yes you need to specify every route. Actually you define the normal routes too with the resource command.
There is a specific wildcard command to allow access of any action, but it is only for debug purposes, because it allows access to actions you may not want to be accessible:
match ':controller(/:action(/:id(.:format)))'

Weird Routing Error in Rails When Manually Adding View

I am not sure what I am doing wrong but when I manually add the view "blah.html.erb" to my project and then visit myproject/dog/blah. It says the following:
Routing Error
No route matches "/dog/blah"
There is an action defined in DogController called "blah" which is the following:
def blah
end
NOTE: I add the view using TextMate. I add a new blank file. I think there is some wrong encoding attached to the .html.erb file.
For clarity, you need to either have each action listed explicitly in your routes.rb file; or you need a wildcard pattern to match the controller and action.
What's in your routes.rb file?
Better yet, you need to have something like this
match "/dog/blah", :to => "dog#blah", :as => :dog_blah
This tells your rails app that the url /dog/blah maps to the blah action in your DogController, and the :as option will give you a named route that you can use in your view in this case dog_blah_path.

Resources