RMcNairn
RMcNairn

Reputation: 491

passing variable into partial

I have the following for and partial setup but i keep on getting an error, that the partial does not recognise the variable |builder|.

Form

<%= simple_form_for @firm  do |f| %>

<%= f.fields_for :events do |builder| %>
          <%= render 'event_fields', :builder => builder  %>
  <% end %>

<%= end %>

_events_fields partial

<fieldset>
<%= builder.input :name, :collection => ['Applications Open', 'Applications Close', 'Traineeship Starts'] %>
<%= builder.text_field :start_at, :class => 'datepicker' %>
<%= builder.hidden_field :all_day, :value => true %>
<%= link_to "remove", '#', class: "remove_fields" %>

any idea how i should be padding the variable across? or if in fact this is the correct way of doing it? It would be really helpful is someone could help me understand a little more about how and why you need to do this.

Upvotes: 1

Views: 468

Answers (2)

Rokibul Hasan
Rokibul Hasan

Reputation: 4146

partial_name would be replace with actual partial name or partial name with directory name.

We would provide the data_object object to the partial, available under the local variable data_variable in the partial and is equivalent to:

<%= render partial: "partial_name", locals: { data_variable: data_object } %>

Upvotes: 0

Darren Coxall
Darren Coxall

Reputation: 1208

You need to tell it that it is a partial view and pass in a hash to the locals option. Like so:

<%= simple_form_for @firm  do |f| %>
    <%= f.fields_for :events do |builder| %>
        <%= render partial: 'event_fields', locals: {builder: builder} %>
    <% end %>
<% end %>

If you're using Ruby 1.8 then:

<%= simple_form_for @firm  do |f| %>
    <%= f.fields_for :events do |builder| %>
        <%= render :partial => 'event_fields', :locals => { :builder => builder } %>
    <% end %>
<% end %>

Upvotes: 3

Related Questions