Rails 3.2.3 - how to setup routes - ruby-on-rails

Here are the url I need to be in my Rails application
site.com
site.com/page/11
site/tags/my_tag
site/tags/my_tag/page/123
where page and tags are actions of HomeController. So how must the file routes.rb look like in this case?

You probably should start with:
match '/page', :to => 'home#page'
match '/tags', :to => 'home#tags'

Related

Rails 4 and Jammit routing issue - should not use match

I've upgraded to Rails 4.0.5 and i'm using Jammit 0.6.6.
When starting the server i get an error :
/home/haimh/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.5/lib/action dispatch/routing/mapper.rb:191:in normalize_condition!:You should not use the match method in your router without specifying an HTTP method.
Looking at the stack trace i see that Jammit's routes.rb file is using the old routing API.
Is there any way of resolving this issue, beside updating the code manually in Jammit's routes.rb file?
use the via
like this:
adding , via => [:get, :post] to the end
match ':controller(/:action(/:id))' , :controller=> /admin\/[^\/]+/, :via => [:get,:post]
if you dont want to use match , you will have to have your routes inside the
resources :controller do
get :action, :on => :collection
end

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 3.2 Routing Error

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'

How can I rename a Rails controller with a route?

I have a controller in a Rails 3 app named "my_store." I would like to be able to use this controller as is, except replacing "my_store" in all the URL's with another name. I do not want to rename the controller file, and all the references to it. Is there a clean way to do this with just a routing statement?
If you use RESTful routes:
resources :another_name, :controller => "my_store"
Otherwise:
match "another_name" => "my_store"
If your routes are RESTful, this is pretty easy.
resources :photos, :controller => "images"
You can see how to do this and other helpful Rails routing information in the Rails routing guide.
Update, the other guys are correct, to replace all references you would change the resources name and corresponding controller in routes.rb! My answer is only good to set a specific route.
Yup, you would do this in your routes.rb using the :as option to specify
example:
match 'exit' => 'sessions#destroy', :as => :logout
source

Using non-English controllers in rails3

I have to following problem:
If i want to follow the Rails naming convention i have to use the plural version of the model's name as my controller name.
Example:
rails g scaffold_controller Content
In this case i have a model(Content) and the previous command is going to generate a controller with the name Contents.
So what if I have a German website and I would like to use site.tld/inhalt/something-something instead of site.tld/contents/something-something.
I was thinking about two solutions:
a, use the German version of the word when I am generating the controller(afaik it is not supported by the scaffold_controller generator
b, generate the whole site(models, controllers) in English and route the specific request to a certain controller with named routes like:
match 'logout', :to => 'sessions#destroy'
In this case I can't use RESTful routes like:
resources :products
What do you think?
You can use routing configuration to change the url that matches this controller:
resources :contents, :path_names => { :new => 'neue', :edit => 'bearbeiten' }, :path => 'inhalt'
The contents_path method in your view will still work, but it'll output /inhalt as required and that will match. The /contents path won't match though. So if you call edit_content_path(#content) you'll get something like /inhalt/1/bearbeiten.
See Rails routing guide for more details.

Resources