Rails 3,将局部变量传递给部分[重复]

2023-12-31

可能的重复:
Rails:对将局部变量传递给局部变量的语法感到困惑 https://stackoverflow.com/questions/4402556/rails-confused-about-syntax-for-passing-locals-to-partials

我想将局部变量(模型中没有相关字段)传递给局部变量。

# infos/index.html.erb

<%= render :partial => 'info', :locals => {:info => first, :img_style => "original"} %> 

:img_style 将是图像的 html 样式。

# infos/_info.html.erb
<% first = @infos.shift %>
<%= image_tag(info.image.url, :class => img_style),  info %> 
# and here goes code for normal iteration
<% @infos.each do |e| %>
# etc

但它不起作用,它返回错误:

# GET /infos/
undefined local variable or method `img_style' for #<#<Class:0xc471f34>:0xc470cc4>

不做多余的部分就可以完成吗?

对不起我的英语不好。 :P

EDIT:

模型信息没有 :img_style 字段

# db/schema.rb
  create_table "infos", :force => true do |t|
    t.string   "title"
    t.text     "description"
    t.integer  "place_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.text     "short"
  end

EDIT2:

甚至简单

<%= img_style %>

不工作。

应用程序堆栈跟踪

app/views/infos/_info.html.erb:3:in `_app_views_infos__info_html_erb___1029249744_92158380_1472718'
app/views/infos/index.html.erb:7:in `_app_views_infos_index_html_erb__184644561_92172050_0'
app/controllers/infos_controller.rb:8:in `index'

EDIT3:

Views

# infos/index.html.erb
<div >
  <h1><%= t('info.infos') %></h1>
  <div id="left">
    <% first = @infos.shift %>
    <div>
      <% @aimg_style = "original"%>
      <%= render 'info', :locals => {@img_style => @aimg_style } %>
    </div>
    <ul>
      <% @infos.each do |e| %>
        <li>
          <div>
            <%= render :partial => 'info', :object => e %>
          </div>
        </li>
      <% end %>
    </ul>
    <%= will_paginate @infos %>

# infos/_info.html.erb
<%#= link_to thumbnail(info, "listTabsImg", false, img_style), info %>
  <%#= image_tag(info.image.url()) %>
  <%= img_style %>
<p>
  <strong class="nameBox"><%= link_to info.title, info %></strong>
  <span><%= info.short %>...</span>
  <%= link_to "#{t('more')} »", info %>
</p>

FINALLY

这不起作用:

# infos/index.html.erb
<% first = @infos.shift %>
<div class="boxEvent">
  <% @aimg_style = "original"%>
  <%= first %>
  <%= render 'info', :locals => {:info => first, :img_style => @aimg_style } %>
</div>

这有效:

# infos/index.html.erb
  <% @infos.each do |e| %>
    <li>
      <div class="boxEvent">
        <%= render :partial => 'info', :locals => {:info => e, :img_style => "original"} %>
      </div>
    </li>
  <% end %>

有人知道为什么吗?


实际上我只是在 Rails 3 中使用这个语法:

render "a_partial", :a_local_variable => whatever, :another_variable => another
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails 3,将局部变量传递给部分[重复] 的相关文章

随机推荐

  • Javascript:Unshift() 导致无限循环,但不明白为什么

    我正在尝试创建一个函数 它接受一个数组并返回偶数在奇数之前的数组 我想我应该遍历数组并用模检查每个数字的整除性 如果它是偶数 我会将该数字移到数组的前面并继续 我已经删除了 unshift 来记录它的迭代 所以我知道问题不在于我的循环 A
  • CMake GET_RUNTIME_DEPENDENCIES 通过 lib(导入库)链接时找不到 dll 库?

    构建操作系统 Windows 10 Cmake 3 16 3 I use target link libraries链接第 3 方 lib文件到我的 dll图书馆 但是当我使用GET RUNTIME DEPENDENCIES安装我的dll
  • grep 是什么风格的正则表达式?

    我猜它不是 Perl 兼容的正则表达式 因为有一种特殊的grep具体是PCRE 什么是grep最相似于 有没有什么特殊的癖好grep我需要知道什么 我习惯了 Perl 和pregPHP 中的函数 默认 GNUgrep行为是使用稍微有味道的变
  • 反应本机转换 - 错误找不到预设“babel-preset-react-native-stage-0

    我开始退出世博会 经过如此多的努力 我能够解决所有构建问题 当我使用 sudo react native run android 运行应用程序时 我开始出现以下错误 Error The development server returned
  • 如何将行自动复制到新工作表VBA Excel

    我试图auto copy一行从master spreadsheet到另一个spreadsheet 当输入值在master等于X So if X被输入到Column A in the master then auto copy分开spread
  • 如何从文本文件中获取特定行? [复制]

    这个问题在这里已经有答案了 我不知道如何从文件中获取特定的文本行 假设文本文件是 1 john 2 mark 3 Luke 如何获取文本文件的第二行 标记 我只需要阅读它 而不是编辑它 int n 2 String lineN Files
  • jQuery 中 $.when.apply(null, a method) 的含义是什么?

    我正在读取 jQuery 中的延迟对象 谁能告诉我以下两种调用方式有什么区别 when apply null a method done function success callback when a method done functi
  • 如何在 EF6 Code First 中将泛型类型与数据库上下文结合使用

    例如 假设我有 4 个不同的实体 每个实体都实现一个将实体添加到数据库的 Add 方法 public class Profile public void Add this dbContext Profile Add this this db
  • 什么是 NMAKE 以及如何使用它?

    什么是 NMAKE 以及如何使用它 NMAKE 有好的教程吗 NMAKE http msdn microsoft com en us library dd9y37ha aspx是微软的实现make http en wikipedia org
  • 使用 QGLShaderProgram 将自定义类型(结构)uniform 从 Qt 传递到 GLSL

    我定义了一个包含两个向量的光参数结构 该结构体在 C 和 GLSL 中都以类似的方式定义 注意 QVector3D封装 3floats not doubles C 主机程序 struct LightParameters QVector3D
  • UIActivityIndi​​catorView 未在 UIAlertView 上正确显示

    我正在编写一个 iOS 应用程序 我必须显示一个带有微调器的 UIAlertView 有时 当我尝试在警报中心添加微调器时 通常会出现另一个警报 不完全是规则 但这就是我注意到它失败的方式 我通过延迟微调器的创建部分解决了这个错误 这是我的
  • 如何用单个循环运行二维数组?

    我想知道我是否可以用一个循环而不是两个循环来写这个东西 for int row 0 row lt matrix length row for int col 0 col lt matrix 0 length col if row col 2
  • TaskExecutor 不工作 Spring 集成

    我已经使用任务执行器设置了文件轮询器 ExecutorService executorService Executors newFixedThreadPool 10 LOG info Setting up the poller for di
  • Mongodb - “递归”删除空字段?

    这是一个基于的问题MongoDb 删除所有为空的字段 https stackoverflow com questions 25287204 mongodb remove all fields that are null 引用的帖子仅提供了删
  • 如何判断字符串是否已经经过 URL 编码?

    如何检查字符串是否已被编码 例如 如果我编码TEST I get TEST 3D 3D 如果我再次对最后一个字符串进行编码 我得到TEST 253D 253D 在这样做之前我必须知道它是否已经编码 我已经保存了编码参数 我需要搜索它们 我不
  • ios Swift 商品未添加到购物车

    当我点击下面的 添加 按钮时 产品不会添加到购物车 并且结帐 0 不会增加到结帐 1 等 我做错了什么 阅读 Larme 的第一条评论后 原始问题得到解决 这是一个菜鸟错误 我忘记将 cell delegate self 放入 tableV
  • 什么是数据库文件系统?

    我对什么是数据库文件系统知之甚少 有人可以向我解释一下数据库文件系统到底是什么 以及它的应用程序是什么吗 它与传统的文件系统有何不同 我怎样才能建造它 典型的文件系统 nix ms dos 等 按层次结构组织文件 例如 c 代表层次结构的顶
  • 如何防止 .htaccess 在特定目录中使用?

    我有一个网站 可以说 http www example com 我正在使用重写模块 但我有一个子文件夹forum example com 我不想要 htaccess要影响这个目录 我该怎么做 If your forum domain com
  • 将参数发送到 Web 服务

    开始之前 我正在使用 Objective C 为 Iphone 编程 我已经使用 NSURLRequest 和 NSURLConnection 实现了对 Web 服务函数的调用 然后该函数返回一个包含我需要的信息的 XML 代码如下 NSU
  • Rails 3,将局部变量传递给部分[重复]

    这个问题在这里已经有答案了 可能的重复 Rails 对将局部变量传递给局部变量的语法感到困惑 https stackoverflow com questions 4402556 rails confused about syntax for