I’m trying to use multiple instance of the same class in a view with rails.
Basically i need to show a Branch with all its attributes and in the same page i have a form_for that need an empty Branch object. The problem is that when i create the empty Branch instance in the controller “@newBranch” the view can’t access the first one anymore
here what I do in the controller:
def show
@customer = Customer.find(params[:id])
@branches = @customer.branches
@newBranch = @customer.branches.new #this is for the form_for
@newContact = @newBranch.build_contact #this is for the fields_for
end
if i try to use a singular item of the collection @branches for example:
<div class = "branch_container">
<%= render :partial => "customers/branch", :collection => @branches %>
</div>
and then inside the branch partial:
<%= branch.contact.name %>
i have the message:
“undefined method `name’ for nil:NilClass”
All the models associations work fine and if i don’t instantiate @newBranch and @newContact the problem disappear.
Basically i need to use two instance of the same Class (for example “@branches” in one partial and “@newBranch” in another one) in the same view.
What could be the solution?
Thank you.
At the end i created @newBranch and @newContact in the view inside the form in the following way: