How can I access the name of the partial from within the layout in Rails
I have a simple partial/layout situation where the layout is as follows;
<div class="widget">
<%= yield %>
</div>
and then within my view I am rendering a partial as;
<%= render :partial => "my_partial", :layout => "my_layout" %>
From within the layout, I want to be able to add the name of the partial (preferably without passing local variables) such that I can customize the widget using CSS.
Id开发者_开发知识库eally, it would be something like this (from within the layout)
<div class="widget <%= partial.name %>">
Is there anyway to access the name of the partial that is being rendered from within the layout?
Thanks.
Why don't you create a helper? Something such as:
module WidgetHelper
def widget options = {}, &block
content = capture &block
# Add class 'widget' if it's not passed as a class.
(options[:class] ||= {}).tap {|classes| classes << "widget" unless classes.include?("widget")}
content_tag(:div, content, options)
end
end
No need to access a file (partial) and no need for local variables.
sample
= widget :class => "my_custom_class" do
= @product.comments.first.name
Should produce
<div class="my_custom_class widget">Block content</div>
Partial name is something constant. So just write it right in partial
<div class="widget my_partial">
and why you don't like local variables?
精彩评论