Chris Muench
Chris Muench

Reputation: 18318

Ruby On Rails...What do partials have access do (Locals, instance variables...anything?)

In the code snippet below, it appears that the form_user_fields partial does not have access to either patient_form or user_fields, but it does have access to @patient. Is this the expected behavior?

<%= form_for @patient do |patient_form| %>
    <%= patient_form.fields_for :user do |user_fields| %>

        <% render :partial => 'shared/form_user_fields' %>

Upvotes: 1

Views: 465

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

In general, partials have access to the same variables as other views(instance variables from the controller.) They won't have access to variables that you create in other views that include the partials, that is the expected behavior.

If you want access to your local variables, you can pass them to the partial:

<%= render :partial => "shared/form_user_field", :locals => {:user_fields => user_fields, :patient_form => patient_form} %>

Upvotes: 4

Related Questions