Show / hide partial *plus* update database

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

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

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