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

左心房为你撑大大i 提交于 2019-12-12 08:01:14

问题


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.


回答1:


It seems does not work that way in activeadmin. The only instance variable available inside "form" block is @config.

The best way to solve this issue is to use partials as described in "Customizing the Form"

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




回答2:


Just in case someone else stumbles upon this:

controller.instance_variable_get(:@user)

should work as well.




回答3:


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) 



回答4:


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

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



回答5:


Not entirely sure how selects the correct instance variable on the model, but, you could give pretty much any name to the instance variable, i test some cases and it seems that just looks for the one that haves the same model type when you don't specify it, to answer your other question, you have many ways to do it the simples one, just the same name as your instance variable, in your case,

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

the you have

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

and the last method 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, like a form for a 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|
.....

Like i say, i'm not sure how active_admin deals with instance variables, but a least you have multiple options, regards




回答6:


If your goal is to set @user for the show action template, it's not necessary to do so, because active admin is already doing this for you.

If you use member_action, the @user object is there for you, it's called resource.

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

This is another way to pass information from the controller to 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


来源:https://stackoverflow.com/questions/8681568/how-do-i-use-instance-variables-defined-in-the-controller-in-the-view-with-act

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