将参数传递给生成器中的 Rails 3 模板

Mat*_*ins 2 templates code-generation generator ruby-on-rails-3

在 Rails 2 生成器中,可以通过以下方式将数据传递给模板

record.template(
  "src.html.erb",
  "dest.html.erb",
  :assigns => { :id => id, :name => name }
Run Code Online (Sandbox Code Playgroud)

在 Rails 3 中,看起来模板是新方法。不幸的是,它看起来像第三个参数,config,只接受一个:verbose选项。试图通过传递值:assigns似乎不起作用。

有谁知道如何将动态值传递给我的 Rails 3 模板?

小智 5

将参数传递给 Rails 3 中的视图的示例。

https://github.com/rails/rails/blob/master/railties/lib/rails/generators/erb/controller/controller_generator.rb

# ...
actions.each do |action|
  @action = action
  @path = File.join(base_path, filename_with_extensions(action))
  template filename_with_extensions(:view), @path
end
Run Code Online (Sandbox Code Playgroud)

模板:

https://github.com/rails/rails/blob/master/railties/lib/rails/generators/erb/controller/templates/view.html.erb

<h1><%= class_name %>#<%= @action %></h1>
<p>Find me in <%= @path %></p>
Run Code Online (Sandbox Code Playgroud)