Possible Duplicate:
Rails: confused about syntax for passing locals to partialsRails 3中,传递局部变量来局部
欲局部变量(其不具有在模型相关领域)传递给部分。
# 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
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:
查看
# 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 %>
有人知道为什么吗?
哪行代码在哪个文件中? '<%= image_tag'是否放置在'_info'部分或其他地方?当第一个示例调用partial或者从其他地方调用它时,该错误是否会引发?我问你是因为你提到了多余的部分,但你没有说过你从多个地方叫出部分。 – Arsen7 2011-06-08 14:09:50
这可能不算什么 - 因为你的问题已经包含了正确的代码 - 但是使用简写语法会导致失败:'render'info',:locals => {...}'。如果你明确地调用partial,'render:partial =>'info',:locals => {...}',那么我看不出有什么理由不起作用。正如@Arsen7所建议的那样,知道你的部分是如何嵌套在你的模板中是很有帮助的。 – neezer 2011-06-08 14:23:50
@ Arsen7当我从浏览器中获得信息时,会引发错误。我想这样做没有redunant partials(每个部分的每个样式,例如:_info_small.html.erb,_info_original.html.erb等) – 2011-06-08 15:03:12