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.
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.
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.
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'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.
Just in case someone else stumbles upon this:
controller.instance_variable_get(:@user)
should work as well.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With