How to use instance variables defined in a controller in a view using ActiveAdmin?

I have it:

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:

 undefined method `display_name' for nil:NilClass 

which means that @user is zero. I am sure @user is set appropriately (which means the crawler gets the relevant data that exists in db). I think this is due to how ActiveAdmin works, which I am not familiar with. Any thoughts?

Also, I know what I can do show do |user| , but there are more complex things that I use for this, and you need access to the user object in the controller.

+6
source share
6 answers

This does not seem to work in activeadmin. The only instance variable available inside the "form" block is @config.

The best way to solve this problem is to use partial parts as described in "Customizing the Form"

http://activeadmin.info/docs/5-forms.html

+1
source

Just in case, someone stumbles upon this:

 controller.instance_variable_get(:@user) 

should also work.

+26
source

There is a controller in the active administrator, despite this, you cannot pass an instance variable to the arbre part. But you can use the 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 

PS: If you do not want to change params , then all instance variables are stored in @arbre_context.assigns . You can also do the following:

 link_to @arbre_context.assigns[:user].display_name, user_path(@arbre_context.assigns[:user].slug) 
+6
source

Not quite sure how to choose the right instance variable in the model, but you could give almost any instance variable name, I am testing some cases and it seems like it is just looking for one that has the same model type when you don't specify it. to answer another question, you have many ways to make it just the same name as your instance variable, in your case

 row :attr do link_to user.display_name, admin_user_path(user) end 

you have

 row :attr do |any_name| link_to any_name.display_name, admin_user_path(any_name) end 

and the last method that I know you have two escenarios, one for your active_admin files (.rb)

 #eg: admin/user.rb @arbre_context.assigns[:user] 

or in custom .arb views, such as the form for custom collection_action (same, but direct access)

 assigns[:user] 

eg:

 #views/admin/users/new_invitation.html.arb(arbre) or html.erb active_admin_form_for assigns[:user], :url => send_invitation_admin_users_path do |user| .... end form_for assigns[:user], :url => send_invitation_admin_users_path do |user| .... end semantic_form_for assigns[:user], :url => send_invitation_admin_users_path do |user| ..... 

As I said, I'm not sure how active_admin deals with instance variables, but at least you have a few options, welcome

0
source

If your goal is to install @user for the show action template, this is not necessary because the active administrator is already doing this for you.

If you use member_action , the @user object exists for you, it is called resource .

You can define a singleton method on your resource , it will be available in the view. This may make sense in some cases.

This is another way to pass information from the controller into the view.

 member_action :show, method: :get do resource.instance_eval do define_singleton_method('language') do 'English' end end end show do attributes_table do row :name row :email row :id row :language end end 
0
source

Instance variables are defined as helper methods. If you have this defined in your controller, you can access it. Alternatively, you can simply call resource , which will have a reference to the object of the active record.

 ActiveAdmin.register User do controller do def show @user = User.find(params[:id]) show! end end show do attributes_table do row "User" do # note that your have access to 'user' as a method. link_to user.display_name, user_path(user.slug) end end end end 
0
source

Source: https://habr.com/ru/post/904760/


All Articles