加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 3,如果它存在,呈现给定控制器的部分?

发布时间:2020-12-17 03:19:56 所属栏目:百科 来源:网络整理
导读:在我的应用程序的application.html.erb布局中,我希望有一个部分呈现给定视图是否存在.例如. 如果访问者位于http://example.com/users/show,我希望部分/users/_sidebar.html.erb呈现. 但是,如果访问者说,http://example.com/user/locations/san_francisco,我
在我的应用程序的application.html.erb布局中,我希望有一个部分呈现给定视图是否存在.例如.

如果访问者位于http://example.com/users/show,我希望部分/users/_sidebar.html.erb呈现.

但是,如果访问者说,http://example.com/user/locations/san_francisco,我想要部分/users/locations/_sidebar.html.erb进行渲染.

所以这里的事情是,如果没有部分控制器/操作,它将在我的共享目录中呈现一些通用的部分,而我宁愿不使用content_for块丢弃每个单独的视图?

有什么想法吗?

解决方法

Sean Behan就此发表了一篇很棒的文章:

http://seanbehan.com/programming/render-partial-if-file-exists/

我可能会把它转移到帮助器并稍微调整一下:

<%= render_sidebar %>

# This method could use either the rescue or the if file exists technique.
def render_sidebar
  render(:partial => "/#{controller.name}/sidebar"
rescue
  #default side bar
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读