开发者

Configure routes.rb for Rails 3.0

I just moved to using Rails 3.0.3 on my Mac OS X.

When i tried to generate cont开发者_如何学Gorollers and views as follows:

 $rails generate controller testing

it created the controller at the appropriate place. i created a method/action in the same controller called greet and when i tried to access this via the browser at:

 http://localhost:3000/testing/greet

It gave me an error saying:

 Routing Error

 No route matches "/testing/greet"

But if i add the following line in my routes.rb file:

 get 'testing#greet'

it works fine, But, i cannot add the above line for each and every method in my application.

Please help me on how to rectify this problem.

Thanks!


He's not over complicating.

Assuming testing is resoucedful, in routes:

resources :testings do
  # for individual /testing/1/greet
  member do
    get 'greet'
  end

  # on the collection
  collection do
    get 'greet'
  end
end


You are over complicating routing. Please read the Routing Guide for enlightenment.


If you are coming from Rails 2.x to Rails 3.0, your routes.rb file is probably missing this line:

 match ':controller/:action'

In Rails 2.x this line was:

 map.connect ':controller/:action'

Because of the change in the Routing API and introduction of Action Dispatch you need to add the first line in your routes.rb file to map every method automatically.

PS: Please remember that if you need to overwrite the above routing, you'll need to declare routing commands above this line, because priority in Rails Routing takes bottom down approach.

Thanks!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜