문제

This is how I'm doing it now:

link_to "Profile", :controller => "profiles", :action => "asked", :id => @profile
# => <a href="/profiles/asked/1">Profile</a>

Does it make more sense for the URL to be <a href="/profiles/1/asked">Profile</a>? Profile 1 asked some number of questions, so it makes more sense to me for the URL to look like: /:controller/:id/:action.

If you agree, how do I accomplish this?

If you don't agree, please let me know why. (I'm new to Ruby on Rails, so I'm still getting used to MVC conventions.)

Any advice would be great!

도움이 되었습니까?

해결책

Yes you can! This is the case of a named route! All you have to do is, in your route.rb add this link:

map.asked 'profiles/:id/asked', :controller => 'profiles', :action => 'asked'

This route can be invoked with asked_path(:id => @profile) Just change your link_to in the views to:

link_to "Profile", asked_path(:id => @profile)

I don't deserve an upvote for this as i just pulled it out from the routes.rb file itself. If you looked carefully at the autogenerated routes.rb you'll see this:

  # Sample of named route:
  #   map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
  # This route can be invoked with purchase_url(:id => product.id)

If you want to make it more generic, i have not tried it.. but i guess it should work nevertheless:

map.routeany ":controller/:id/:action"

and in the view:

link_to "Route to something...", routeany_path(:controller => "somecontroller", :action => "someaction", :id => @somecontroller)

Anyways, cheers! :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top