将参数传递给生成器中的 Rails 3 模板
在 Rails 2 生成器中,可以将数据传递到模板 按以下方式:
record.template(
"src.html.erb",
"dest.html.erb",
:assigns => { :id => id, :name => name }
在 Rails 3 中,它看起来像 template 是新方法。不幸的是,第三个参数 config
看起来只接受 :verbose
选项。尝试通过 :assigns
传递值似乎不起作用。
有谁知道如何将动态值传递给我的 Rails 3 模板?
In Rails 2 generators, it's possible to pass data to the template in the following manner:
record.template(
"src.html.erb",
"dest.html.erb",
:assigns => { :id => id, :name => name }
In Rails 3, it looks like template is the new method. Unfortunately, it looks like the third parameter, config
, only accepts a :verbose
option. Attempting to pass values via :assigns
doesn't seem to work.
Does anyone know how I can pass dynamic value to my Rails 3 template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将参数传递到 Rails 3 中的视图的示例。
https://github.com/rails/rails/blob/master/railties/lib/rails/generators/erb/controller/controller_generator.rb
模板:
https://github.com/rails/rails/blob /master/railties/lib/rails/generators/erb/controller/templates/view.html.erb
Example of passing parameters to views in Rails 3.
https://github.com/rails/rails/blob/master/railties/lib/rails/generators/erb/controller/controller_generator.rb
Template:
https://github.com/rails/rails/blob/master/railties/lib/rails/generators/erb/controller/templates/view.html.erb
Rails 3 模板可以访问生成器中的任何方法,因此在上面的情况下,如果您的生成器有一个方法
name
和一个方法id
,那么您不必更改模板中的任何内容。查看用于在 Rails 3 中创建生成器的 RailsCast,网址为 http://railscasts。 com/episodes/218-making-generators-in-rails-3。Rails 3 templates have access to any methods within the generator, so in your above case, if your generator had a method
name
and a methodid
, you wouldn't have to change anything in your template. Check out the RailsCast for creating generators in Rails 3 at http://railscasts.com/episodes/218-making-generators-in-rails-3.