开发者

Rails automatically pull out resource from nested resources in routes helpers

I have nested resources like so

resources :users do
  resources :widgets
end

When I have @widget, to get a proper routing from my helpers i need to use user_widget_path(@widget.user, @widget) is there any way to tell rails to automatically pull out the user from @widget object?开发者_高级运维 So i could just use user_widget_path(@widget)


@apneadiving is totally right. But you can improve a little your approach:

 link_to "user", user_widget_path(@widget.user, @widget)

can be presented shorter:

 link_to "user", [@widget.user, @widget]

UPD

Also you can rewrite user_widget_path as you want:

class ApplicationController < ActionController::Base
  helper_method :user_widget_path
  private
  def user_widget_path(widget)
    super(widget.user, widget)
  end
end

You should also rewrite edit_user_widget_path, new_user_widget_path. And better to wrap it as an external Module.


There is no automatic method to do this. But you could create your own application helper, it's pretty straight.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜