Topic: Using RJS to hide only part of a partial?

I have a partial that displays a list of comments. When the user that owns the comment clicks delete, it should hide only that comment. (it is later deleted by my controller)

I'm having trouble since I can't just tell the RJS file which comment in the partial it should hide. With this code, it fades all of the comments out, which is of course not what I want:

RJS:

page[:comments].visual_effect :fade

(:comments is a simple div ID)

My Partial call:
 

<%= render :partial => 'show/comment',
          :collection => @comments_submitted %>

Last edited by zac439 (2007-10-04 15:36:08)

Re: Using RJS to hide only part of a partial?

Try this:

<div id="comment_<%= comment.id %>">

That way you have a tag with the id of the comment in it so you have something to reference when hiding.

Railscasts - Free Ruby on Rails Screencasts

Re: Using RJS to hide only part of a partial?

Interesting! Alright I got the reference to work.

Now I need to add multiple CSS elements that correspond to the comment number?

(example: comment_1, comment_2, etc)

Is there an easier way to do this?

Re: Using RJS to hide only part of a partial?

You can use a class for the CSS part:

# view
<div id="comment_<%= comment.id %>" class="comment">

# css
.comment {
  /* ... */
}

Railscasts - Free Ruby on Rails Screencasts

Re: Using RJS to hide only part of a partial?

In my page source, I'm getting:

<div id="comment_58" class="comment">

Is that correct? It seems it isn't displaying the DIV correctly. Perhaps my CSS file is incorrect? It shows the following:
.comment{
background-color: #FFEBCD;
border: 1px solid black;
margin: 0px 0px 2px 0px;
}

EDIT:

I got the div working again. Now the RJS file seems at fault:

  page[:comment_<%= @comment.id %>].visual_effect :fade

Last edited by zac439 (2007-10-04 17:47:04)

Re: Using RJS to hide only part of a partial?

Solved it myself. I was trying to call the @comment.id in my RJS file when it wasn't defined in my controller.

Oops!

Haha, thanks for the help.