Niki Jackson
Niki Jackson

Reputation: 25

How to pass variables down to partial?

I am New to Ruby on rails, I am using Rails 4.1.9, Ruby 2.0.0 on Windows xp

This is my models

class Migproject < ActiveRecord::Base
    validates :name, presence: true 
        validates :clientname, presence: true
        validates :startdate, presence: true
        #validates :enddate, presence: true
        validates :is_active, presence: true
        validates :statusid, presence: true

        has_many :projectmembers, dependent: :destroy
        accepts_nested_attributes_for :projectmembers, :allow_destroy => true
    end

    class Projectmember < ActiveRecord::Base
        validates :fk_userid, presence: true
        validates :fk_roleid, presence: true

        belongs_to :roles
        belongs_to :users
        belongs_to :migproject  
    end

This is my view

<%= mp.fields_for :projectmembers do |pm| %>
        <%= render "projectmember_fields", :projectmembers => pm %>
      <% end %>

this is my partial

<tr class="fields">
  <td>Select Users :</td><td><%= projectmembers.collection_select :fk_userid, User.all, :pk_userid, :fname, {:prompt => true} %></td>
  <td>&nbsp;&nbsp;&nbsp;&nbsp;Select Role :</td><td><%= projectmembers.collection_select :fk_roleid, Role.all, :pk_roleid, :rolename, {:prompt => true} %></td>
  <td><span class="removefld"><%= projectmembers.hidden_field :_destroy %><%= link_to "Remove" %></span></td>
</tr>

But when i run i get error like this

undefined local variable or method `projectmembers' for #<#<Class:0x4e13680>:0x2ea41e0>

Where am i making mistake?

Is there any other method to add a dynamic field to a form?


This is my application helper

module ApplicationHelper
def link_to_add_fields(name, mp, association)
        new_object = mp.object.send(association).klass.new
        id = new_object.object_id
        fields = mp.fields_for(association, new_object, child_index: id) do |build|
            render(association.to_s.singularize + "_fields", mp: build)
        end
        link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n","")})
    end 
end

Can i render a block of HTML from same page instead of calling partial in helper?

Upvotes: 0

Views: 143

Answers (2)

Benjamin Bouchet
Benjamin Bouchet

Reputation: 13181

You need to write it like this:

<%= render :partial => "projectmember_fields", :locals => {:projectmembers => pm} %>

More info in the chapter "3.4.4 Passing Local Variables" of the guide


EDIT: Just understood. I had this before. You issue comes from the link_to_add_fields method, at the following line:

render(association.to_s.singularize + "_fields", mp: build)

This render call also need to the local variables, like this:

render(:partial => "#{association.to_s.singularize}_fields", :locals => {:projectmembers => build}

Next time, find a way to display the whole backtrace of your error, and see each invoked line of your code.

Upvotes: 6

fedetaglia
fedetaglia

Reputation: 238

Try not to use the model name for the form builder

<%= render "projectmember_fields", pm_form: pm %>

and inside the partial:

<%= pm_form.collection_select ..

Upvotes: 0

Related Questions