ActiveAdmin - How to access instance variables from partial?

I cannot access instance objects in partials. Example:

In the 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 part, I have:

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

This does not work. The only thing that works (which is terrible, since I put the logic into the point of view, is:

  <% @aliases = Alias.where("store_id = ?", params[:id]) %> <% @aliases.each do |alias| %> <li><%= alias.name %></li> <% end %> 
+8
ruby-on-rails ruby-on-rails-3 activeadmin
source share
2 answers

When rendering a partial action, you need to actively define the variables that are provided to this partial action. Change your line

 render :partial => "store_aliases" 

to

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

Inside your partial variables are then available as a local variable (and not an instance variable!). You need to adjust your logic inside the part by removing @ .

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

For further reference see the API documentation (in particular, the section "3.3.4 Passing Local Variables").

+17
source share

You must pass your instance variable in part 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 partial you can use it as a local variable

 <% aliases.each do |alias| %> <li><%= alias.name %></li> <% end %> 
+9
source share

All Articles