Rails 3 Routing problem - ruby-on-rails

I convert the Rails-2 application into Rails-3. In my Rails-2 routing i have the routes like the below
Rails 2
map.connect 'example/:action/:id.:format', :controller => 'Test',:q =>'example-string'
Note: This is working well in Rails-2 application; when the url comes with /example it redirect to Test controller's index action with the parameter q="example-string"
I converted the above to support Rails-3 routes:
match 'example(/:action(/:id.(:format)))',:to => 'Test',:q=>'example-stirng'
The problem is I got the Routing Error /example not found.
How can i change the Rails-2 routes into Rails-3 routes?

You almost got it right. Should be
match 'example(/:action(/:id.(:format)))',:controller => :test, :q=>'example-stirng'
:to => "foo#bar" is a shortcut for :controller => :foo, :action => :bar

Related

Ruby on Rails routing for variable controller

I have a URL I want to be able to redirect to.
Something similar to:
"http://localhost:3000/username/admin/page".
I have a match in routes.rb as:
match ':account/admin/:page' => "admin#index"
I have redirect code:
redirect_to :controller => account.username, :action=>"admin", :page=>"index"
This, however comes up with a routing error:
No route matches {:action=>"admin", :controller=>"sdunn", :page=>"index"}
I know what I have done is wrong, but how can I fix this?
Many thanks.
Route is expecting 2 parameters, first one is :account, second is :page, i think you are only passing :page. I would add :as => 'some_name' to your route and then use _path :
routes.rb
match ':account/admin/:page' => "admin#index", :as => 'my_route'
controller:
redirect_to my_route_path(#user, #page)
my_route_path could be something different depending on your exact route file, so use
rake routes | grep my_route
to see exact name, then add _path to the end.

hyphen resources in rails 3 routes

How is it possible to use hyphen in resources urls?
For example: /my-model/ or /my-model/1.
If I define route as resources :"my-model" I get syntax error because rails generates method def hash_for_my-models_url(options = nil).
I have found the solution:
resources "my-models", :as => :my_models, :controller => :my_models
UPDATE:
As Timo Saloranta said in comment it works without :controller => :my_models in latest Rails 3 versions.
You can use the :as option to configure resourceful routes with hyphenated URLs:
map.resources :my_model, :as => "my-model"
results in
my_model_index GET /my-model(.:format) {:action=>"index",
:controller=>"my_model"}
...etc...
Have you tried a custom route?
map.connect "/my-model/:id", :controller => 'my-model-controller', :action => 'read'
This would invoke the 'read' method of 'my-model-controller.rb'.

Rails 3 routing - passing params from routes.rb

In rails 2.3.5 you could do something like this inside the routes.rb file:
map.root :controller => "pages", :action => "show", :id => 3
In rails 3 I haven't found any way to pass a specific parameter (like in rails 2.3.5 with :id => 3).
I know I can handle it from the controller and have the same result (which I did), but I was wondering if there is a way to do the same thing in rails 3 from the routes.rb or has it changed because it is better practice for some reason?
Are you sure the following doesn't work?
root :to => "pages#show", :id => 3

How do I fix this routing error in Ruby on Rails?

I've put this line in my routes.db file:
map.mything '/mything', :controller => 'mything', :action => 'list'
But I get this error when I go to http://localhost:3000/mything, I get this error:
Unknown action
No action responded to index. Actions: list
Why is it trying to use index instead of list? I thought that by setting
:action => 'list'
it would use the list action? Thanks for reading.
You have to put named routes above the default routes.
I put named routes like these at the top of routes.rb so they always get evaluated first.
ActionController::Routing::Routes.draw do |map|
map.about 'about', :controller => 'home', :action => 'about'
map.contact 'contact', :controller => 'home', :action => 'contact'
# MORE CONFIG
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Agreeing with Jim Schubert, put the named routes above the default routes.
Another likely problem is that you have something like:
map.resources :mything
which is setting an index action on the controller as a result of you scaffolding a model
Sorry for asking a potentially obvious question, but have you tried restarting the app? Certain routes will not register until you restart the application (RESTful resources never need an application restart, but others often do).

Server prefix and rails routes

When i'm starting the server with the path option
script/server --path=/myapp
while having a route
map.route 'foo', :controller => 'bar', :action => 'buzz'
then
ActionController::Routing::Routes.recognize_path('/myapp/foo')
raises an error "No route matched ..."
Question: How can i make Rails built-in routing recognize with path prefix?
Thanks a lot!
Try putting config.action_controller.relative_url_root = "/myapp" in environments.rb and start your server normally.
Then Rails will append /myapp/ to all your routes
There is actually a path_prefix available for routes so you can do something like this:
map.foo, 'foo', :controller => 'bar', :action => 'buzz', :path_prefix => 'myapp'
That should give you a route for /myapp/foo
thanks a lot for your answers!
unfortunately i can't use the :path_prefix option in map.foo, because it's not always the case (the end-user should be responsible for setting or not the prefix while not worring about any routes).
i fingered out following:
path = '/myapp/foo'
if relative_url_root = ActionController::Base.relative_url_root
path.sub!(/\A#{relative_url_root}/i, '')
end
params = ActionController::Routing::Routes.recognize(path)
# => {:controller => 'bar', :action => 'buzz'}

Resources