Using non-English controllers in rails3 - ruby-on-rails

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.

Related

Ruby on Rails - How do I use dashes instead of underscores in the url?

When I ran the rails generate controller Pages index sample_page it created my two controllers. I can see the page at localhost:3000/sample_page, but how do I change it to show dashes instead of underscores in the url ie: localhost:3000/sample-page
Rails doesn't like it when you create a controller with dashes, so I have to use underscore. What do I need to add to my routes.rb file?
Thanks!
use path in routing
resources :pages do
collection do
get :contact_us, path: "contact-us"
end
end
Note that you should probably just create one controller for your pages.
To answer your question, you can do some custom routes eg:
match '/about-us' => 'pages#about'
match '/contact-us' => 'pages#contact'
match '/terms-and-conditions' => 'pages#terms'

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

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

Routing: prefix item name before controller resources

I am trying to get the following routes to work in my Rails 3 app.
Scenario:
I have the following controllers in my app:
Practices
Doctors
Patients
Prescriptions
Putting up :resources for each of them in routes.rb gives me routes from
example.com/practices
example.com/doctors/1/edit etc
What I'd like to have however is the following resourceful routes, for example:
example.com/james_practice/docs that translates to the doctors controller
example.com/james_practice/awesome_prescriptions that routes to the prescriptions controller etc. etc.
both of these giving me access to :practice name and furthermore route the correct controller with all the helpers like edit_docs_path(doctor) etc.
How would I go about this? I've used
resources :prescriptions, :path => "/:practice/awesome_prescriptions"
but although it showed the correct routes in "rake routes", it still wouldn't work as expected.
I think this is the route you're looking for:
scope :path => ":practice" do
resources :docs, :controller => "doctors"
resources :awesome_prescriptions, :controller => "prescriptions"
end
By the way, you didn't give me the example of Patients, so I didn't put it there.
map.resources is just a pattern, more like a shortcut. If you want to have URLs like in your examples you should use "named routes". A good screencast for Rails 2.x: http://railscasts.com/episodes/34-named-routes
Edit: Read section 3.2 in Rails Tutorial: http://guides.rubyonrails.org/routing.html
p.s. Also you'll face a case where people use "." in their names and it'll cause problems.
Here's my route for "tags"
map.resources :tags, :requirements => { :id => %r([^/;,?]+) }

How to remove controller name in REST design in Rails3?

Given a User resource, it goes like this
/user/:shortname
But how can the controller name be removed to get just
/:shortname
How can I declare this in routes.rb while keeping all CRUD functionality instant?
Updated: After reading this I'm moving to Sinatra over Rails to handle this API-like design better.
Define a custom match:
match ':shortname' => 'users#action'
Replace action in users#action with the name of the action that is supposed to receive the request. Just remember to place it in the appropriate order in your routes file. Rails looks at each line of your routes file starting at the top and selects the first matching route. ':shortname' would match any first-level path, including /users! So put it below any routes using a first-level path, which would include all of your resource routes. Here's an example:
resources :users
resources :posts
match '/blog' => 'posts#index'
match ':shortname' => 'users#action'
In routes, you should be able to do something like
resource :users, :path => '/:shortname'
Try that out and rake routes to see if that comes out as expected.

Resources