如何将变量传递给 render_to_string? [英] How to pass variables to render_to_string?

查看:44
本文介绍了如何将变量传递给 render_to_string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试执行以下操作

@message = render_to_string ( :sender => sender, :template => "template" )          

但是当在模板中访问@sender 时,结果是 nil:NilClass.仔细检查我是否通过了正确的变量,这完全没问题.也许还有其他方法可以将变量传递给 render_to_string?

But when accessing @sender in template it turns out to be nil:NilClass. Double checked if I pass the right variable and it's totally fine. Maybe there are other way to pass variables to render_to_string?

推荐答案

这可能是您使用的语法.尝试使用 :locals 参数:

It might be the syntax you're using. Try using the :locals argument:

@m = render_to_string :template => "template", :locals => {:sender => sender}

然后你只需要访问sender(没有@)作为模板内的局部变量.

Then you just need to access sender (without an @) as a local variable inside the template.

这篇关于如何将变量传递给 render_to_string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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