将动态变量传递给partial以在模态中渲染

时间:2014-02-15 14:22:34

标签: ruby-on-rails modal-dialog twitter-bootstrap-3 local-variables dynamic-variables

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

“#<#: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>    

1 个答案:

答案 0 :(得分:0)

我不知道你在这里想做什么


<强>循环

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

解决问题的方法是设置实例变量(不需要),或使用其他persistent data type。我这样做:

#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

为您进行一些重构:

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