ActiveAdmin — How to access instance variables from partials?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 03:41:52

When rendering a partial you need to actively define the variables that are given to that partial. Change your line

render :partial => "store_aliases"

to

render :partial => "store_aliases", :locals => {:aliases => @aliases }

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 @.

<% aliases.each do |alias| %>
  <li><%= alias.name %></li>
<% end %>

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:

ActiveAdmin.register Store do
  sidebar "Aliases", :only => :edit do
    @aliases = Alias.where("store_id = ?", params[:id])
    render :partial => "store_aliases", :locals => { :aliases => @aliases }
  end 
end

Then in your partial you will be able to use it as local variable

<% aliases.each do |alias| %>
  <li><%= alias.name %></li>
<% end %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!