Your Friend
Your Friend

Reputation: 61

error while rendering a partial in rails3

I am trying to render a partial in rails using the normal

<%=render 'subtasks/descform %> command

Now the issue is that this descform partial is being called from inside another partial which is 'subtasks/subtask' and their are many subtasks.

So the subtask partial is also getting rendered from the task partial like this

<% task.subtasks.each do |subtask| %>
<%=render :partial=>subtask %>
<%end%>

So the problem is in the desc form,it has a form like this

<%=form_for subtask,:remote=>:true,:method => :put do |f| %>
          <%= f.text_area :description,rows:'5',cols:'12',:class=>'myarea'%>
          <%= f.submit "save",class:'btn btn-success col-md-4' %>
         <a data-id='<%=subtask.id %>' href="#" class="myclose"><i class="fa fa-times fa-lg" style="margin:10px auto;"></i></a>
<% end %>

So,when i am trying to render this form i am getting an error

undefined local variable or method `subtask' for #<#:0x458f6b8>

Can somebody please explain the cause of this error and how to fix this.

Upvotes: 0

Views: 49

Answers (3)

Marco Prins
Marco Prins

Reputation: 7419

You just need to pass the variable to the partial, like so:

<%=render 'subtasks/descform', :subtask => subtask %>

Then it will be accessible in your partial as subtask

Upvotes: 1

Coenwulf
Coenwulf

Reputation: 1937

In the task partial:

<%= render :partial => "subtask", :collection => task.subtasks %>

In the subtask partial:

<%=render :partial => 'subtasks/descform', :locals => {:subtask => subtask} %>

Upvotes: 0

usha
usha

Reputation: 29369

Pass the subtask object as a local variable to descform partial. First to subtask partial and then to descform

<% task.subtasks.each do |subtask| %>
   <%=render :partial => 'subtask', :locals => {:subtask => subtask} %>
<%end%>

And inside subtask partial, pass it on to descform

 <%=render :partial => 'subtasks/descform', :locals => {:subtask => subtask} %>

You have to mention partial when you pass in locals. For eg, the following will not work

<%=render 'subtasks/descform', :locals => {:subtask => subtask} %>

Upvotes: 1

Related Questions