Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render comments outside of resource show #3327

Closed
kochis opened this issue Aug 12, 2014 · 8 comments
Closed

Render comments outside of resource show #3327

kochis opened this issue Aug 12, 2014 · 8 comments

Comments

@kochis
Copy link

kochis commented Aug 12, 2014

I can't seem to find a way to render the ActiveAdmin comments outside of the show action. Is this even possible?

I've tried calling the active_admin_comments method directly on ActiveAdmin::Comments::ShowPageHelper, but can't seem to get it to work.

What I'm trying to do is render the comments in the view from a member_action method. Is there perhaps a better way to do this?

@ccallebs
Copy link
Contributor

I think this should work:

  ActiveAdmin::Comments::Views::Comments.active_admin_comments_for(@your_resource)

@kochis
Copy link
Author

kochis commented Aug 14, 2014

I tried that in both view, and controller methods, and got an undefined method error. I think this tends to be an issue when calling this method outside the context of the ActiveAdmin show method.

@zorab47
Copy link
Contributor

zorab47 commented Aug 14, 2014

Arbre components can only be rendered within an Arbre context. The blocks for member_action and collection_action run directly within the controller and do not provide a context for Arbre components. It is possible to render an Arbre view template such as custom_action.html.arb and within that template active_admin_comments_for(resource) is available.

# custom_action.html.arb
active_admin_comments_for(resource)

A second method is to create an Arbre::Context within the view rendered by another template language like ERB.

<h2>Custom Action ERB</h2>
<%= 
  Arbre::Context.new({}, self) do
    active_admin_comments_for(resource)
  end
%>

This method works for all Arbre components provided by ActiveAdmin. See Arbre::Context for more information.

@kochis
Copy link
Author

kochis commented Aug 14, 2014

Thanks @zorab47, that was able to solve my issue 👍

@kochis kochis closed this as completed Aug 14, 2014
@BRZInc
Copy link

BRZInc commented Jan 21, 2015

Thank you very much for this hint!

I've tried to use this advice and finally comments are shown just once on the page, but at the same time comments addition form is missing.

Could you advice me, how to fix this?

@andywenk
Copy link
Contributor

andywenk commented Mar 9, 2015

@BRZInc I assume you try to add the comments form to the edit view. That will not work since you cant put a form into a form. The edit view looks like this with (not working) the comments form:

form do |f| 
  # form fields
  active_admin_comments_for(resource)
end

You will see the comments and the form but not the comments form will be submitted but the whole resource form.

If you just want to see the comments, you can recreate the comments by yourself without creating a form:

form do |f|
  # form fields

  panel "Comments", class: 'comments player panel' do
    ActiveAdmin::Comment.where(resource_id: params[:id]).each do |comment|
      admin = AdminUser.find(comment.author_id)
      div class: 'active_admin_comment' do
        div class: 'active_admin_comment_meta' do
          h4 class: 'active_admin_comment_author' do
            a admin.email, href: admin_admin_user_path(comment.author_id)
          end
          span l admin.created_at
        end
        div class: 'active_admin_comment_body' do
          text_node "<p>#{comment.body}</p>".html_safe
        end
      end
    end

    text_node "#{button_to 'Neuer Kommentar', edit_admin_player_path(player.id)}".html_safe
  end
end

You could also take the result from active_admin_comments_for(resource) and throw away the form in the string. But that's fishy imho.

EDIT: see the next two comments ...

@timoschilling
Copy link
Member

@andywenk I think don't need the join.html_safe and you can use each

@andywenk
Copy link
Contributor

andywenk commented Mar 9, 2015

@timoschilling yup you are right - that was still the code when I debugged the values without creating any HTML. Updated my comment. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants