26

可能重复:
Rails:对将局部变量传递给局部变量的语法感到困惑

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

# 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

编辑:

模型信息没有 :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

编辑2:

甚至简单

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

编辑3:

意见

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

最后

这不起作用:

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

有人知道为什么吗?

4

5 回答 5

50

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

render "a_partial", :a_local_variable => whatever, :another_variable => another
于 2011-06-08T14:29:20.597 回答
12

这应该有效:

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

有了这个部分:

# infos/_info.html.erb
<%= image_tag(info.image.url, :class => img_style),  info %>

但是,如果您调用部分错误,请尝试将其作为部分错误:

# infos/_info.html.erb
<%= image_tag(info.image.url(img_style)),  info %>
于 2011-06-08T15:08:08.500 回答
5

我在这上面花了几个小时;在一个视图中, render :partial => ? , :locals => (....} 如果你首先调用:

render :partial => ? without a locals hash

然后使用本地哈希调用渲染:partial => ?,本地哈希不会传递给视图。

如果第一次调用包含设置了相同变量的本地哈希,'' 那么使用 REAL 值的第二次调用将起作用。

于 2012-03-01T03:12:19.963 回答
2

您的代码(您在第一行中显示的代码)应该可以工作。没有错误。我什至自己测试过它,它对我有用。

此类错误的通常原因是:

  • 变量名拼写错误
  • 也从其他地方调用部分,你不通过:本地。

您的模型没有属性“img_style”这一事实并不重要。

我可以看到infos/index.html.erb你有两个地方可以打电话render :partial => 'info', :object => ...,而你没有 :locals 这里。只是行号与您之前发布的堆栈跟踪不匹配,因此很难说是哪个调用导致了问题。

我猜对方法的两个调用都render需要更正。

在第一个中,要具体,并调用render :partial => "info"而不是render "info". 这可能是 Rails 中的一个错误,但出于某种奇怪的原因,在这种情况下, :locals 似乎没有传递给视图。

在第二次调用中render添加 :locals。

于 2011-06-08T15:12:40.483 回答
1

I believe the problem is the trailing , info call. It is looking in the info object for img_style. You don't need it.

I love using partials but recently have seen the beauty in helper methods too, especially in simple renderings like this one.

def info_image(info, class_name="original")
   image_tag(info.image.url, :class => class_name)
end

then just

<%= info_image(first) %>

or

<%= info_image(first, "anotherclass") %>

in the view. Much cleaner.

If ever it becomes more complex. You'll only have to change code in one place.

于 2011-06-08T15:26:46.497 回答