ActiveAdmin——如何从部分访问实例变量?
我似乎无法访问部分实例对象。示例:
在控制器中我有:
ActiveAdmin.register Store do
sidebar "Aliases", :only => :edit do
@aliases = Alias.where("store_id = ?", params[:id])
render :partial => "store_aliases"
end
end
然后在 _store_aliases.html.erb 部分我有:
<% @aliases.each do |alias| %>
<li><%= alias.name %></li>
<% end %>
这不起作用。唯一有效的事情(这是可怕的,因为我将逻辑放入视图中是这样的:
<% @aliases = Alias.where("store_id = ?", params[:id]) %>
<% @aliases.each do |alias| %>
<li><%= alias.name %></li>
<% end %>
I can't seem to access instance objects in partials. Example:
In controller I have:
ActiveAdmin.register Store do
sidebar "Aliases", :only => :edit do
@aliases = Alias.where("store_id = ?", params[:id])
render :partial => "store_aliases"
end
end
Then in the _store_aliases.html.erb partial I have:
<% @aliases.each do |alias| %>
<li><%= alias.name %></li>
<% end %>
This doesn't work. The only thing that does work (which is horrible to do as I'm putting logic in a view is this:
<% @aliases = Alias.where("store_id = ?", params[:id]) %>
<% @aliases.each do |alias| %>
<li><%= alias.name %></li>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
渲染局部时,您需要主动定义赋予该局部的变量。将您的行更改
为
在您的部分内,然后可以将变量作为局部变量(而不是实例变量!)进行访问。您需要通过删除
@
来调整部分内部的逻辑。如需进一步阅读,请参阅 API 文档(特别是“3.3.4 传递局部变量”部分) ”)。
When rendering a partial you need to actively define the variables that are given to that partial. Change your line
to
Inside your partial the variables is then accessible as a local variable (not an instance variable!). You need to adjust your logic inside the partial by removing the
@
.For further reading have a look at the API documentation (specifically the section "3.3.4 Passing Local Variables").
您必须将实例变量传递给您的局部变量才能在那里使用它:
然后在您的局部变量中您将能够将其用作局部变量
You have to pass your instance variable to your partial in order to use it there:
Then in your partial you will be able to use it as local variable