开发者

Rails 3, rendering a partial for the given controller if it exists?

开发者 https://www.devze.com 2023-02-17 18:59 出处:网络
In my application.html.erb layout for my app, I want to have a partial that renders if it exists for the given view. for example.

In my application.html.erb layout for my app, I want to have a partial that renders if it exists for the given view. for example.

If the visitor is at http://example.com/users/show, I'd want the partial /users/_sidebar.html.erb to render.

But if the visitor were at sa开发者_如何学JAVAy, http://example.com/user/locations/san_francisco, I'd want the partial /users/locations/_sidebar.html.erb to render.

So the thing here is that if there were no partial for that controller/action it would render some generic partial in my shared directory, and I'd rather not litter every single view with content_for blocks ya know?

Any ideas guys?


My solution is a bit different. Throw this in your application helper:

  def render_partial_if_exists(base_name, options={})

    file_name           = ::Rails.root.to_s+"/app/views/layouts/_#{base_name}.html.erb"
    partial_name        = "layouts/#{base_name}"
    else_file_name      = ::Rails.root.to_s+"/app/views/layouts/_#{options[:else]}.html.erb"
    else_partial_name   = "layouts/#{options[:else]}"

    if File.exists?(file_name)
      render :partial => partial_name
    elsif (options.key?(:else) and !options[:else].nil? and File.exists?(else_file_name))
        render :partial => else_partial_name
    end
  end

Then in your view:

<%= render_partial_if_exists "page_#{controller.action_name}_sidebar", :else => "page_sidebar" %>

In an edit action, if "layouts/page_edit_sidebar" exists it renders it, otherwise it will render a standby "layouts/page_sidebar"


Sean Behan has a great post on exactly this:

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

I might move it to a helper and tweak it a bit to:

<%= 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
0

精彩评论

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

关注公众号