Ruby 在Espresso中,如何在模板中使用动作参数而不将其分配给实例变量?
我有很多操作通过方法参数接收参数:Ruby 在Espresso中,如何在模板中使用动作参数而不将其分配给实例变量?,ruby,Ruby,我有很多操作通过方法参数接收参数: class App < E map '/' def read page @page = page # etc. render end def profile user @user = user # etc. render end end class-App
class App < E
map '/'
def read page
@page = page
# etc.
render
end
def profile user
@user = user
# etc.
render
end
end
class-App
如何访问模板中的
页面
、用户
等,而不将它们分配给实例变量?您在这里至少有两个选项
最简单的方法是使用操作参数
:
def read page
# use action_params[:page] in templates
end
请注意,这只适用于Ruby 1.9
在Ruby 1.8中,使用操作参数[0]
另一种方法是将参数作为上下文变量传递:
def profile user
render :user => user
# in templates, user is available as #user getter
# rather than @user instance variable
end
查看源代码,如果将哈希传递给render函数,它会将这些键作为局部变量分配给视图。差不多
render(:foo => "Bar")
在行动中,然后在视图中,您可以
<%= foo %>
Bar