4

我在一个部分上有以下代码,它动态地生成一个与 pin 相关的步骤列表。引脚和台阶都有图像。用户可以将多个步骤添加到图钉,然后此视图会动态生成这些步骤的视图。对于每个步骤,都有关联的图像,我想将其作为模态弹出。挑战是我不断收到以下错误:

“#<#:0x00000102f498d8> 的未定义局部变量或方法‘步骤’”

当我存根渲染部分时,模态显示基本上是空白的,但可以工作。任何想法我该怎么做?

<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>    
4

1 回答 1

0

我不明白你在这里想要做什么


环形

正如评论中提到的,你有一个循环通过你的步骤。但是,在循环之外,您希望显示部分

解决您的问题的方法是设置一个实例变量(不需要),或者使用另一种持久数据类型。我会这样做:

#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 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 %>
于 2014-02-16T12:57:10.923 回答