user1560922
user1560922

Reputation: 267

How can I pass a variable from the controller to a partial?

I rendering a view partial like this.

<%= 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 %>

but the variable @test1 is empty. Do you have any idea ?

Upvotes: 0

Views: 58

Answers (1)

sorens
sorens

Reputation: 5060

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

and change _position.html.erb to

<%= test1 %>

Upvotes: 1

Related Questions