And*_*rew 3 routes views ruby-on-rails path ruby-on-rails-3
我有兴趣为我的布局创建一个标准,其中每个视图将被包装在根据特定约定命名的容器中.
例如,如果我有:
resources :foos
Run Code Online (Sandbox Code Playgroud)
我希望视图包含在div中,如下所示:
<div id="foos_view"> foos#index here </div>
<div id="foo_view"> foos#show here </div>
<div id="new_foo_view"> foos#new here </div>
<div id="edit_foo_view"> foos#edit here </div>
Run Code Online (Sandbox Code Playgroud)
所以,基本上我想使用路线名称,但用'path'代替'view'.
我的问题是,有没有办法获取当前视图的路由名称?
IE浏览器.如果我的请求是example.com/foos/new什么,我可以在视图或控制器中调用哪些将返回new_foo_path?
谢谢!
我想不出一个简单的方法,因为路径助手只是调用path_for()并填写所需的参数,所以没有方法调用反向执行.
但是,这是Ruby,因此编写快速帮助程序以返回所需的字符串相当容易.可以通过controller.controller_name操作名称和操作名称访问当前控制器名称controller.action_name.
这样的事情应该对你有用:
def html_id
string = ""
if controller.action_name =~ /new|edit/
string += controller.action_name + "_"
end
if controller.action_name =~ /index|create/
string += controller.controller_name
else
string += controller.controller_name.singularize
end
return string += "_view"
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5248 次 |
| 最近记录: |