这次介绍视图页面block的helper方法
我们可以在application_helper里定义一个admin_area helper方法:
[code]
module ApplicationHelper
def admin_area(&block)
if admin?
concat content_tag(:div, capture(&black), :class => 'admin'), block.binding
else
nil
end
end
end
[/code]
在我们的页面中可以这样使用:
[code]
<% admin_area do %>
<%= link_to "Edit Task", edit_task_path(@task)
<% end %>
[/code]
这样,如果当前用户为admin,则在页面中显示编辑链接并加上class为admin的div,否则不显示
我们还可以定义一个side_section helper方法:
[code]
module ApplicationHelper
def side_section(&block)
@side_sections ||= []
@side_sections << capture(&block)
end
end
[/code]
这样当我们在页面中使用side_section方法并且参数为一个block时,会生成一个@side_sections的实例变量
这样我们在layout中就可以留出side_section的位置来显示
我们可以在application_helper里定义一个admin_area helper方法:
[code]
module ApplicationHelper
def admin_area(&block)
if admin?
concat content_tag(:div, capture(&black), :class => 'admin'), block.binding
else
nil
end
end
end
[/code]
在我们的页面中可以这样使用:
[code]
<% admin_area do %>
<%= link_to "Edit Task", edit_task_path(@task)
<% end %>
[/code]
这样,如果当前用户为admin,则在页面中显示编辑链接并加上class为admin的div,否则不显示
我们还可以定义一个side_section helper方法:
[code]
module ApplicationHelper
def side_section(&block)
@side_sections ||= []
@side_sections << capture(&block)
end
end
[/code]
这样当我们在页面中使用side_section方法并且参数为一个block时,会生成一个@side_sections的实例变量
这样我们在layout中就可以留出side_section的位置来显示