简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM