0

这就是我现在的做法:

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

URL 更有意义<a href="/profiles/1/asked">Profile</a>吗?Profile 1问了一些questions,所以 URL 看起来像这样对我来说更有意义:/:controller/:id/:action.

如果您同意,我该如何实现?

如果您不同意,请告诉我原因。(我是 Ruby on Rails 的新手,所以我仍然习惯于 MVC 约定。)

任何建议都会很棒!

4

1 回答 1

1

是的你可以!这是命名路线的情况!您所要做的就是在您的 route.rb 中添加此链接:

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

可以使用 ask_path(:id => @profile) 调用此路由,只需将视图中的 link_to 更改为:

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

我不值得为此投赞成票,因为我只是从 routes.rb 文件本身中提取了它。如果你仔细查看自动生成的 routes.rb 你会看到:

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

如果您想让它更通用,我还没有尝试过..但我想它应该可以工作:

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

在视图中:

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

总之,加油!:)

于 2010-06-02T02:10:31.843 回答