从辅助方法渲染部分 [英] Rendering partials from a helper method

查看:49
本文介绍了从辅助方法渲染部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用辅助方法渲染局部变量,您还可以从调用辅助方法的视图中传递局部变量?例如,当我直接在视图中包含此代码时,它会正确呈现部分:

Is it possible to render a partial using a helper method where you can also pass local variables from the view in which the helper method is called? For example, when I include this code directly in the view, it renders the partial properly:

 <%= render :partial => "add_round", :locals => { :f => f } %>

然后我把它移到了一个辅助方法:

Then I moved it to a helper method:

def addRound
  render :partial => "add_round", :locals => { :f => f }
end

然后我再次从视图中调用它:

Then I called it from the view again with:

 <%= addRound %>

这不适用于代码中包含的 :locals => { :f => f }.它返回了这个错误:未定义的局部变量或方法`f'.但是,addRound 方法确实呈现了以下内容:

This did not work with the :locals => { :f => f } included in the code. It returned this error: undefined local variable or method `f'. However, the addRound method did render something with the following:

def addRound
  render :partial => "add_round"
end

以这种方式编写它允许我渲染不需要传递局部变量的部分(例如纯文本字符串).但是我怎样才能让它与 :locals => { :f => f } 一起工作?还有其他的写法吗?

Writing it this way allowed me to render partials that didn't require the local variables to be passed through (such as plain text strings). But how can I get it to work with the :locals => { :f => f } included? Is there another way to write that?

非常感谢.

推荐答案

您需要将 f 传递给 addRound

def addRound(f)
  render partial: "add_round", locals: { f: f }
end

在视图中

<%= addRound(f) %>

这篇关于从辅助方法渲染部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆