Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use instance variables, defined in the controller, in the view with ActiveAdmin?

I have this:

ActiveAdmin.register User do
  controller do
    def show
      @user = User.find(params[:id])
      show! 
    end
  end

  show do
    attributes_table do
      row "User" do
        link_to @user.display_name, user_path(@user.slug) 
      end
    end
  end
end

But when I load the page, I get an error saying:

undefined method `display_name' for nil:NilClass

which means that @user is nil. I am positive that @user is set appropriately (meaning the finder is getting appropriate data that exists in the db). I'm thinking it has something to with how ActiveAdmin works that I'm unfamiliar with. Any thoughts?

Also, I know I could do show do |user|, but there are more complicated things I am using this for and need access to the user object in the controller.

like image 909
jfedick Avatar asked Dec 30 '11 16:12

jfedick


People also ask

Are instance variables set within a controller method accessible within a view?

Are instance variables set within a controller method accessible within a view? Yes, any instance variables that are set in an action method on a controller can be accessed and displayed in a view.

How do instance variables work in Ruby?

An instance variable in ruby has a name starting with @ symbol, and its content is restricted to whatever the object itself refers to. Two separate objects, even though they belong to the same class, are allowed to have different values for their instance variables.

How do I find the instance of a variable?

Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name.

What is an instance variable in Ruby on Rails?

What's an instance variable? In the Ruby programming language, an instance variable is a type of variable which starts with an @ symbol. Example: @fruit. An instance variable is used as part of Object-Oriented Programming (OOP) to give objects their own private space to store data.


2 Answers

Just in case someone else stumbles upon this:

controller.instance_variable_get(:@user)

should work as well.

like image 57
wpp Avatar answered Sep 20 '22 16:09

wpp


There is controller in active admin, despite this you can not pass instance variable to arbre part. But you can use params hash for this:

ActiveAdmin.register User do
  controller do
    def show
      params[:user] = User.find(params[:id])
      show! 
    end
  end

  show do
    attributes_table do
      row "User" do
        link_to params[:user].display_name, user_path(params[:user].slug) 
      end
    end
  end
end

P.S.: If you don't want to change params, then all instance variables are stored in @arbre_context.assigns. You may also do like:

link_to @arbre_context.assigns[:user].display_name, user_path(@arbre_context.assigns[:user].slug) 
like image 28
Jiemurat Avatar answered Sep 20 '22 16:09

Jiemurat