0

I have a list of items that I render via a call to a partial

render @items

each item is placed inside a div and has a JS 'disable' link

<div id="item_id_<%=item.id%>
   <%=item.name>
   <%= link_to disable_item_path(item), :remote => true %>
</div>

Inside the controller, I change the flag of the item in the database to False, and want to re-render the page and remove the item from the list.

I have been doing so by the following code in disable.js.coffee

$('#items_list').html("<%= escape_javascript(render(:partial => @items)) %>")

My question: I would like simply to hide the specific item DIV, and not re-render all the items on the page (why? because I think it's better coding).

How do I do that? I tried passing @item_id to the coffeescript, and doing something like

$('item_id'+@item_id).hide

but from reading around here it seems like it's the wrong way of doing so.

Thanks!

2 Answers 2

1

To simply hide the div, put this in your disable.js.coffee file:

$("#item_id_<%= item.id %>").hide()
Sign up to request clarification or add additional context in comments.

1 Comment

ID selectors should start with a #
0

Why not have a parent #items div with each child .item class, then reference by $('#items > .item').hide or something like that.

At the moment you have

<div id="item_id_1">...</div>
<div id="item_id_2">...</div>
<div id="item_id_3">...</div>

And so on.

Instead, you can have:

<div id="items">
  <div class="item">...</div>
  <div class="item">...</div>
  <div class="item">...</div>
</div>

5 Comments

Unfortunately I am not familiar enough with CSS and how it works, can you please elaborate? How would the browser know which .item to hide?
Updated, hope it makes sense. From what you've said that should work. It's possible you may need to use the .each method, just see the jQuery documentation on .each if so.
Yes, I can do that easily, but how then do I hide a variable item in the list (for example, the 2nd one)? what JQuery do I need to use for that?
I did the changes as Dylan suggested, and then used #$('.row').remove() - the result is that all my rows are getting removed, and a specific one like I need...
I found a way of doing so, wrapping my view with <%= div_for(item) do %> and in the disable.js file: $('#<%= dom_id(@item) %>').hide()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.