Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何干燥嵌套资源的url_帮助程序并使它们知道父类?_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何干燥嵌套资源的url_帮助程序并使它们知道父类?

Ruby on rails 如何干燥嵌套资源的url_帮助程序并使它们知道父类?,ruby-on-rails,Ruby On Rails,在Rails 3.2应用程序中,我可以通过执行以下操作来删除包含当前对象链接的视图代码 #helper def current_url(new_params) url_for params.merge(new_params) end #shared partial <%= link_to "JSON", current_url(:format=>:json) %> #shared/_comments_link.html.erb <%= link_to "#{res

在Rails 3.2应用程序中,我可以通过执行以下操作来删除包含当前对象链接的视图代码

#helper
def current_url(new_params)
  url_for params.merge(new_params)
end

#shared partial
<%= link_to "JSON", current_url(:format=>:json) %>
#shared/_comments_link.html.erb
<%= link_to "#{resource.class.name}'s Comments", #{resource}_comments_path(#{resource}) %>
#助手
定义当前url(新参数)
参数合并的url_(新参数)
结束
#共享部分
:json)%%>
但是如果我需要链接到一个嵌套的资源呢?例如,用户和对象模型都可以评论的多态注释模型。这是用嵌套路由设置的,因此要链接到注释视图,我将使用

#user view
<%= link_to "User's Comments", user_comments_path(@user) %>
#object view
<%= link_to "Object's Comments", object_comments_path(@object) %>
#用户视图
#对象视图
有没有一种方法可以在不将局部数据传递给局部数据的情况下使其干燥?我想要像这样的东西

#helper
def current_url(new_params)
  url_for params.merge(new_params)
end

#shared partial
<%= link_to "JSON", current_url(:format=>:json) %>
#shared/_comments_link.html.erb
<%= link_to "#{resource.class.name}'s Comments", #{resource}_comments_path(#{resource}) %>
#共享/_注释_link.html.erb

但是我不知道如何让url\u助手知道父类是什么

def link_to_comments(resouce)
  name = "#{resource.class}'s Comments"
  path = send :"#{resource.class.name.underscore}_comments_path", resource
  link_to name, path
end