4

I have the following Code on a partial that dynamically generates a list of associated steps to a pin. Both pins and steps have image(s). Users are able to add multiple steps to a pin and then this view dynamically generates a view of these steps. For each step there are associated image(s) which I'd like to pop-up as a modal. The challenge is I keep getting the following error:

"undefined local variable or method `step' for #<#:0x00000102f498d8>"

When I stub out rendering the partial, the modal appears basically blank but works. Any ideas How do I do this?

<div class="col-md-6">
      <% (0..(Step.where("pin_id = ?", params[:id]).count)-1).each do |step| %>
          <div class="col-md-12">
          <div class="well"> 
              <ul class="nav pull-right">
                <% if current_user == @pin.user %>
                        <%= link_to edit_step_path((Step.where("pin_id = ?", params[:id]).fetch(step)), :pin_id => @pin.id) do %>
                      <span class="glyphicon glyphicon-edit"></span>
                          Edit
                      <% end %> |
                 <%= link_to Step.where("pin_id = ?", params[:id]).fetch(step), method: :delete, data: { confirm: 'Are you sure?' } do %>
                      <span class="glyphicon glyphicon-trash"></span>
                      Delete
                    <% end %> |
                     <%= link_to new_step_image_path(:step_id => Step.where("pin_id = ?", params[:id]).fetch(step), :pin_id => @pin.id) do %>
                              Add
                      <span class="glyphicon glyphicon-picture"></span> 
                      <% end %> 
                <% end %> 
                <% if StepImage.where("step_id = ?", Step.where("pin_id = ?", params[:id]).pluck(:id).fetch(step)).count == 0 %> 

                <% else %>
                | <a href="#StepImageModal" data-toggle="modal"> 
              <span class="glyphicon glyphicon-picture"></span> (<%= StepImage.where("step_id = ?", Step.where("pin_id = ?", params[:id]).pluck(:id).fetch(step)).count %> ) </strong>
               </a>  
               <% end %>
              </ul>
              <strong> Step <%= step+1 %>    
              <p>
              <%= Step.where("pin_id = ?", params[:id]).pluck(:description).fetch(step) %>
              </p>
          </div>
        </div>
      <%end %>
    </div>

<div class="modal fade" id="StepImageModal">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
    <h3>Modal Header</h3>
  </div>
  <div class="modal-body">
      <%= render :partial => 'pins/step_images',
                 :locals => { :step => step } %>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
  </div>
</div>    
Mike Causer
  • 8,196
  • 2
  • 43
  • 63

1 Answers1

0

I don't get what you're trying to do here


Loop

As mentioned in the comments, you've got a loop which goes through your steps. However, outside the loop, you want to display the partial

The solution to your woes will either be to set an instance variable (not desired), or use another persistent data type. I'd do this:

#app/controllers/steps_controller.rb
def action
    @step = {}
end

#app/views/steps/action.html.erb
<% (0..(Step.where("pin_id = ?", params[:id]).count)-1).each do |step| %>
   <% @step[step.id.to_sym] = step.details %>
<% end %>

<%= render :partial => 'pins/step_images', :locals => { :step => @step } %>

unless

Some refactoring for you:

<% unless StepImage.where("step_id = ?", Step.where("pin_id = ?", params[:id]).pluck(:id).fetch(step)).count == 0 %> 
     | <%= link_to "#StepImageModal", data: { toggle: "modal"} do %> 
           <span class="glyphicon glyphicon-picture"></span>(<%= StepImage.where("step_id = ?", Step.where("pin_id = ?", params[:id]).pluck(:id).fetch(step)).count %> ) </strong>
       <% end %> 
<% end %>
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Rich, thanks. Definitely understand that comment now. This code sits on a pin page where the url is http://localhost:3000/pins/107. So what I'm trying to do is have a pop-out modal of the images associated to each Step. |step| could simply be |i| as you likely understand. So what I want to do is show how many images are associated with each step on the pin page, but then when you click on the link to the modal display all the images in code which is contained in a partial. I definitely agree creating an instance variable is not ideal (although in honesty I don't know what that is). – user3313516 Feb 16 '14 at 17:52
  • Couldn't you just pass the `@pin.images` object? This will bring back all the images associated with the pin – Richard Peck Feb 17 '14 at 09:54
  • Thanks for following up. Let me try to explain more. It could be an architectural issue - I'm still very new to coding. Pins have multiple images. Steps have multiple images. Steps belong to pins. On the pin page I show all pin images and all steps. I do not want to show step images or the layout will suffer. I'm trying to put them inside a modal. Steps are dynamically generated on the page I posted because there is no set number associated with a pin. I see how the modal is outside the loop adding to the challenge. I think I need some type of javascript for "onclick" but don't know it – user3313516 Feb 17 '14 at 19:52
  • Hi All. Thanks for your help! The note about it being outside the loop was correct. Needed to put the modal inside the loop. Next question is the controls are changing a seperate modal on the underlying page... oh the pleasures of coding. – user3313516 Feb 19 '14 at 01:11