如何将变量从控制器传递给部分?

时间:2012-08-05 15:18:01

标签: ruby ruby-on-rails-3 controller

我像这样渲染视图。

<%= render :partial => 'resources/positions', :controller => 'resources',
                         :action => 'this_is_a_test',
                         :locals => {:id_resource => 42} %>

resources_controller.rb

def this_is_a_test
    @test1 = "batman"
    render :partial => 'positions'
  end

_positions.html.erb

<%= @test1 %>

但变量@ test1为空。你有什么主意吗 ?

1 个答案:

答案 0 :(得分:1)

def this_is_a_test
  render :partial => 'positions', :locals => { :test1 => "batman" }
end

并将_position.html.erb更改为

<%= test1 %>