简体   繁体   中英

How to add views for custom member action in active admin?

I have to add view for custom action which is member action, and want to display association records on it. Is there way to add custom view instead of just adding html.erb in admin's view folder?

I dont want to add or create html files but by using the active admins helpers.

The member action in nothing else like a controller action, thats mean you can do the same things in it.

You can use thinks like:

render text: "Hello world!"

Or if you want a complex markup:

message1 = "Hello"
@message2 = "world!"
view = Arbre::Context.new(message: message, self) do
  h1 do
    span message
    span @message
  end
end
render body: view.to_html # or .to_s

You can use the following code for the render html for rails 4.1 :

render html: '<html><body>Some body text</body></html>'.html_safe ## Add html_safe

But, if you use rails 4.2 , so you can use the following:

render text: '<html><body>Some body text</body></html>'

I think the following answer is very useful for your question.

Ended up with adding the following in html.erb file in admin/user/messages.html.erb

<% view = Arbre::Context.new({messages: @messages, user: @user}, self) do

      panel "Sent Messages" do
        paginated_collection(messages, download_links: false) do
          table_for collection do
            column :id
            column :content            
          end
        end
      end
    end
%>

<%= view.to_s %>

Depending on your namespace (ActiveAdmin is on /admin in my case) you can create the folder app/views/admin in the same way you would in the rest of your application.

For example, if you have a resource User and an action apply_discount

ActiveAdmin.register User do
  member_action :apply_discount, method: [:get, :put] do
    if request.get?
      render :apply_discount
    else
      # TODO ...
    end
  end    
end

you could put your ARBRE view file into app/views/admin/users/apply_discount.html.arb -> notice the extension is ARB, not ERB - though ERB should work too according to the docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM