Ruby-on-rails Rails 3,将局部变量传递给部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6279651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 3, passing local variable to partial
提问by nothing-special-here
Possible Duplicate:
Rails: confused about syntax for passing locals to partials
I want to pass local variable(which doesn't have relevant field in model) to partial.
我想将局部变量(模型中没有相关字段)传递给部分变量。
# infos/index.html.erb
<%= render :partial => 'info', :locals => {:info => first, :img_style => "original"} %>
:img_style will be html style for image.
: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
But it does not work, it returns error:
但它不起作用,它返回错误:
# GET /infos/
undefined local variable or method `img_style' for #<#<Class:0xc471f34>:0xc470cc4>
It can be done without making redundant partials?
可以在不制作多余部分的情况下完成吗?
Sorry for my English. :P
对不起我的英语不好。:P
EDIT:
编辑:
Well model Info don't have :img_style field
那么模型信息没有 :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:
编辑2:
Even simple
再简单不过
<%= img_style %>
don't works.
不工作。
Application Stack Trace
应用程序堆栈跟踪
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:
编辑3:
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
最后
This don't works:
这不起作用:
# infos/index.html.erb
<% first = @infos.shift %>
<div class="boxEvent">
<% @aimg_style = "original"%>
<%= first %>
<%= render 'info', :locals => {:info => first, :img_style => @aimg_style } %>
</div>
This works:
这有效:
# infos/index.html.erb
<% @infos.each do |e| %>
<li>
<div class="boxEvent">
<%= render :partial => 'info', :locals => {:info => e, :img_style => "original"} %>
</div>
</li>
<% end %>
Anybody know why?
有人知道为什么吗?
回答by d11wtq
I actually just use this syntax in Rails 3:
我实际上只是在 Rails 3 中使用这种语法:
render "a_partial", :a_local_variable => whatever, :another_variable => another
回答by Devin M
This should work:
这应该有效:
<%= render :partial => "info", :locals => { :img_style => "original" } %>
With this partial:
有了这个部分:
# infos/_info.html.erb
<%= image_tag(info.image.url, :class => img_style), info %>
However if you are calling the partial wrong then try this as the partial:
但是,如果您调用部分错误,请尝试将其作为部分:
# infos/_info.html.erb
<%= image_tag(info.image.url(img_style)), info %>
回答by John
I've spent several hours on this; in a view,
render :partial => ? , :locals => (....}it seems if you first call:
我在这上面花了几个小时;从某种意义上说,
render :partial => ? , :locals => (....}如果您首先调用:
render :partial => ? without a locals hash
and THEN call render :partial => ?with a locals hash the locals hash IS NOT passed to the view.
然后:partial => ?使用本地哈希调用渲染本地哈希不会传递给视图。
If the first call includes a locals hash with the same variables set to ''then the second call with REAL values will work.
如果第一个调用包含一个具有相同变量设置的 locals 散列,''那么具有 REAL 值的第二个调用将起作用。
回答by Arsen7
Your code (the one you have shown in the first line) should work. There is no error. I have even tested it myself and it works for me.
您的代码(您在第一行中显示的代码)应该可以工作。没有错误。我什至自己测试过它,它对我有用。
The usual reasons for such errors are:
出现此类错误的通常原因是:
- misspelled variable name
- calling the partial also from some other place, where you do not pass a :local.
- 拼错的变量名
- 也从其他地方调用部分,在那里你不传递:本地。
The fact that your model does not have the attribute "img_style" is not important.
您的模型没有属性“img_style”这一事实并不重要。
I can see that in your infos/index.html.erbyou have two places where you call render :partial => 'info', :object => ...and you do not have :locals here. Just the line numbers do not match with the stacktrace you have posted earlier, so it's hard to say which call causes the problem.
我可以看到,在您infos/index.html.erb那里,您有两个地方可以打电话,render :partial => 'info', :object => ...而这里没有 :locals。只是行号与您之前发布的堆栈跟踪不匹配,因此很难说是哪个调用导致了问题。
I guess both calls to the rendermethod need corrections.
我想对render方法的两个调用都需要更正。
In the first one, be specific, and call render :partial => "info"instead of render "info". This may be a bug in Rails, but for some strange reason, :locals seem to not be passed to the view in this case.
在第一个中,要具体,并调用render :partial => "info"而不是render "info"。这可能是 Rails 中的一个错误,但出于某种奇怪的原因,在这种情况下 :locals 似乎没有传递给视图。
In the second call to renderjust add the :locals.
在第二次调用中render只添加 :locals。
回答by natedavisolds
I believe the problem is the trailing , infocall. It is looking in the infoobject for img_style. You don't need it.
我相信问题是尾随, info电话。它正在info对象中寻找img_style。你不需要它。
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.
如果它变得更加复杂。您只需在一处更改代码。

