【问题标题】:passing dynamic variable to partial for rendering in a modal将动态变量传递给部分以在模态中呈现
【发布时间】:2014-02-15 14:22:33
【问题描述】:

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

"# 的未定义局部变量或方法 `step'"

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

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

【问题讨论】:

  • 你能贴出Step的型号代码吗?
  • 模态体内的变量 step 实际上不可用...您已经在循环之外编写了它
  • 我可以保存 |step|循环内某处的变量,以便它可用于模态?

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


【解决方案1】:

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


循环

正如 cmets 中所提到的,您有一个循环执行您的步骤。但是,在循环之外,您要显示部分

解决问题的方法是设置一个实例变量(不需要),或者使用另一个 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 %>

【讨论】:

  • 有钱,谢谢。现在绝对理解该评论。此代码位于 URL 为 localhost:3000/pins/107 的 pin 页面上。所以我想要做的是有一个与每个步骤相关联的图像的弹出模式。 |步骤|可能只是 |i|正如你可能理解的那样。所以我想要做的是显示有多少图像与 pin 页面上的每个步骤相关联,但是当您单击模态的链接时,会显示包含在部分中的代码中的所有图像。我绝对同意创建一个实例变量并不理想(虽然老实说我不知道​​那是什么)。
  • 你不能只传递@pin.images 对象吗?这将带回与该图钉关联的所有图像
  • 感谢您的跟进。让我试着解释更多。这可能是一个架构问题——我对编码还是很陌生。 Pins 有多个图像。步骤有多个图像。台阶属于针脚。在 pin 页面上,我显示所有 pin 图像和所有步骤。我不想显示步骤图像,否则布局会受到影响。我正在尝试将它们放在模态中。步骤是在我发布的页面上动态生成的,因为没有与 pin 关联的设置编号。我看到模态如何在循环之外增加了挑战。我想我需要某种类型的 javascript 用于“onclick”,但不知道
  • 大家好。谢谢你的帮助!关于它在循环之外的注释是正确的。需要将模态放入循环中。下一个问题是控件正在更改底层页面上的单独模式......哦,编码的乐趣。
猜你喜欢
  • 2016-07-20
  • 1970-01-01
  • 2023-03-18
  • 2011-05-18
  • 1970-01-01
  • 2015-07-05
  • 2021-10-28
  • 2015-11-06
  • 2023-03-20
相关资源
最近更新 更多