I have a view folder product_types. The name of the controller file is product_types_controller and the class I have is ProductTypesController. I'd normally keep the _ format that rails prefers but I need to keep the current page syntax for search index reasons.
How do I get this controller to show up for mysite.com/product-types and all pages in the folder product_types to appear for mysite.com/product-types/some-page? Do I need to name the pages with - or should I use the _ syntax as well and just change the routes.
This is for a Rails 2.3.8 site.
Thanks
For Rails 3 you have to do this differently:
resources "product-types", :as => :product_types, :controller => :product_types
In case you're using namespaces in your routes in Rails 3, you can use the following for dashes in urls:
namespace :product_types, :path => "product-types" do
If you're using RESTful routes, you can do this:
map.resources :product_types, :as => 'product-types'
I hope this helps!
In rails 3 you can do:
resources :product_types, :path => '/product-types'
Related
Routing makes zero sense to me, is there an easy way to convert this:
#map.resources :bicycles, :controller => 'store/bicycles', :path_prefix => 'store'
thanks in advance
The controller option is also available in Rails 3 and the path_prefix can be achieved with scope. The below is the conversion of the route to Rails 3
scope 'store' do
resources :bicycles, :controller => 'store/bicycles'
end
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'
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
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([^/;,?]+) }
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.