0
votes

I use rails infrequently, so pardon if this is obvious. I've looked at the other questions, am working from Ruby On Rail Guide, and it looks like I'm doing this correctly, but I still can't get my local variables to work.

todays/show.html.erb

<p>
  <strong>Tasks:</strong>  
  <%= render partial: "next_steps/next_step_today", 
        collection: @today_lines %>
</p>

next_steps/_next_step_today.html.erb

  <p>
    <%= render partial: "next_steps/next_step",
            locals: {next_step: today_line.next_step} %>
    <%= link_to 'x', today_line, method: :delete, data: { confirm: 'Are you sure?' } %>
  </p>

next_steps/_next_step.html.erb

<span class="<%= next_step.complete ? "completed_next_step" : ""%> next_step_span_<%= next_step.id%>">
<%= form_for(next_step, 
      :remote => true, 
      :html => {:'data-type' => 'json', 
                :multipart => true, 
                :id => "edit_next_step_#{next_step.id}_#{rand()}"}
      )  do |f| %>
    <%= f.hidden_field( :id, :value => next_step.id) %>
    <%= f.hidden_field( :note_id, :value => next_step.note_id) %>
    <%= f.hidden_field( :content, :value => next_step.content) %>
    <%= f.hidden_field( :due_date, :value => next_step.due_date) %>
    <%= f.check_box( :complete, :class => "next_step_complete_button" ) %>
    <%= next_step.content %>
<% end %>
</span>

I know that the last partial _next_step.html.erb was working before I starting trying to add partials for extra flexibility. Now I can't seem to get the locals passed in correctly.

My previous working code was

todays/show.html.erb

<p>
<strong>Tasks:</strong> 

<% @today_lines.each do |today_line| %>
  <p><%= render today_line.next_step %></p>
<% end %>
</p>

Error message

NameError in Todays#show

Showing /.../app/views/next_steps/_next_step_today.html.erb where line #3 raised:

undefined local variable or method `today_line' for #<#:0x007f930adbd860> Extracted source (around line #3):

<p>
  <%= render partial: "next_steps/next_step",
          locals: {next_step: today_line.next_step} %>
  <%= link_to 'x', today_line, method: :delete, data: { confirm: 'Are you sure?' } %>
</p>

Any ideas?

EDIT: FINAL WORKING CODE

todays/show.html.erb

<p>
  <strong>Tasks:</strong>
  <%= render partial: "next_steps/next_step_today", 
    collection: @today_lines %>
</p>

_next_step_today.html.erb

  <p>
    <%= render "next_steps/next_step",
        next_step: next_step_today.next_step %>
    <%= link_to 'x', next_step_today, method: :delete, data: { confirm: 'Are you sure?' } %>
  </p>

collection: does require that you use the partial file name for the local var.

I also found in my searching that if you don't use partial: you can also omit locals: and just use the variable name instead, which is cleaner. This change is shown in _next_step_today.html.erb. I wish I could also name the collection variable, instead of using the partial file name, or instead of using an each, but this is close enough, and working.

2

2 Answers

1
votes

The name of the variable within the partial is derived from the name of the partial itself, not from the name of the collection.

Your _next_step_today.html.erb should then be the following:

<p>
  <%= render partial: "next_steps/next_step",
        locals: {next_step: next_step_today.next_step} %>
  <%= link_to 'x', next_step_today, method: :delete, data: { confirm: 'Are you sure?' } %>
</p>

Edit: if you want to stick to the @collection convention, that is

1
votes

Show needs to specify the key name that the partial will use. So:

<%= render partial: "next_steps/next_step_today", 
    today_lines: @today_lines %>

Fix the spelling in next_step_today partial as well:

  <%= render partial: "next_steps/next_step",
      locals: {next_step: today_lines.next_step} %>