# Show / hide partial \*plus\* update database

**URL:** <https://www.ruby-forum.com/t/show-hide-partial-plus-update-database/116058>\
**Category:** Rails\
**Created:** [October 1, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/show-hide-partial-plus-update-database/116058 "2007-10-01T23:42:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [October 1, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/show-hide-partial-plus-update-database/116058/1 "2007-10-01T23:42:41Z")

</div>

Hello,

I have a partial that I would like to allow users to either display or  
not display a partial, by clicking a gif.

I want it so that when they click an “x”, the partial is hidden, and  
the “x” becomes a check mark. Then, when they click the check mark,  
the partial is displayed, and the check mark becomes an “x” again.  
Here is my strategy:

– make a helper that toggles a style=“display: none” inside the div  
that contains my partial, depending on whether show / hide is set to 1  
or 0 in my database for a given user  
– hook up my check marks to a link\_to\_remote action in my controller,  
that toggles show / hide in the database, and does a page.replace\_html  
for my partial

(Code is below)

When I click once on my gif, my partial disappears. When I look at  
the html source, the style=“display: none” is not in the source – I  
have to refresh the entire page to see style=“display: none”

When I click my gif again, my partial does not reappear. When I check  
the source, I can see the partial – but i have to do a page refresh  
in order to see the partial in my browser.

Help very very much appreciated.

Charlie

helper

def hidden\_div\_if(condition, attributes = {} )  
if condition  
attributes[“style”] = “display: none”  
end  
attrs = tag\_options(attributes.stringify\_keys)  
“#{attrs}”  
end

view

\>

action

def hide\_newslistings  
id = params[:id]  
@search\_term = SearchTerm.find(id)  
@user\_search\_term = UserSearch.find\_by\_search\_term\_id(id)

## this toggles show / hide

```
if @user_search_term.hide_newslistings == 0
  @user_search_term.hide_newslistings = 1
else
  @user_search_term.hide_newslistings = 0
end
  @user_search_term.save

```

## paginator

```
@user_newslisting_pages, @user_newslistings =

```

paginate(:user\_newslistings,  
:conditions =\> [“user\_newslistings.search\_term\_id = ? and  
user\_newslistings.deleted = ‘0’”, id],  
:include =\> :newslisting,  
:order =\> ‘modification\_date DESC’,  
:per\_page =\> 5)

```
render :update do |page|
  page.replace_html 'content_box_5', :partial =>

```

‘news\_listings’, :url =\> {“page=?”, page}, :layout =\> false

```
end

```

end

---

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [October 1, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/show-hide-partial-plus-update-database/116058/2 "2007-10-01T23:53:12Z")

</div>

On 1 Oct 2007, at 22:28, charlie caroff wrote:

> have to refresh the entire page to see style=“display: none”

How would it? your code isn’t replacing content\_box\_5, just the html  
inside it.

> When I click my gif again, my partial does not reappear. When I check  
> the source, I can see the partial – but i have to do a page refresh  
> in order to see the partial in my browser.

I suspect the same is happening. You’ll need stuff to hide/show divs  
in your rjs (Element.hide & Element.show for example).

Fred

---

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [October 2, 2007, 1:10am UTC](https://www.ruby-forum.com/t/show-hide-partial-plus-update-database/116058/3 "2007-10-02T01:10:39Z")

</div>

> How would it? your code isn’t replacing content\_box\_5, just the html  
> inside it.

Ok, that did the trick; I thought the entire div was being replaced,  
not just the html inside it.

All I had to do to make it work was to put the html I wanted replaced  
inside that div, and everything works fine.

Thanks,

Charlie
