Dynamically construct RESTful route using Rails

試著忘記壹切 提交于 2019-12-06 11:20:52

def get_link(resource)
  link_to "#{resource.capitalize}", send("#{resource}_path")
end
def get_link(resource)
  link_to(resource.to_s.titleize, send("#{resource}_path"))
end

The to_s call on resource is to support passing symbols as resource. So

get_link("foos")

will work and also

get_link(:foos)

If you want to construct a RESTful route with a member:

send("edit_#{resource}_path".to_sym, @resource)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!