Forum: Ruby on Rails Hide parent element

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E4c4a05719644c37f02dc5181de7c8c5?d=identicon&s=25 Seth Buntin (Guest)
on 2006-05-30 22:31
I have some code that calls a partial to display one or more blocks
within a div tag.  I want to have a close button (or cancel link) that
will fade that block but leave the others.  Here is the code:

LINK:
<%= link_to_remote "Add Miscellaneous Link", :url => { :action =>
:add_iyc_link } %>

add_iyc_link.rjs:
page.insert_html :bottom, 'links', :partial => 'add_iyc_link'
page.visual_effect :highlight, 'links'

_add_iyc_link.rhtml:
<p class="iyclink" id="iyclink">
    <%= image_tag "cancel.png", :class => 'close', :onclick => "new
Effect.Fade('iyclink', {duration: 3})" %>
    <label for="">Title</label><br/>
    <%= text_field 'in_your_classroom_link', 'title[]', :size => '30',
:class => 'link' %>
    <br/>
    <label for="">URL</label><br/>
    <%= text_field 'in_your_classroom_link', 'url[]', :size => '50',
:class => 'link' %>
    <br/>
    <label for="">Description</label> <span
class="markdown">[Markdown]</span><br/>
    <%= text_area 'in_your_classroom_link', 'bodytext[]', :cols => '70',
:rows => '5'  %>
</p>

Can this be done?  The problem is when I have two or more blocks and
click the cancel button the first gets faded which is not the one I
want.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-31 04:07
(Received via mailing list)
The problem is that each of your <p> elements all have the same id.  The
javascript works with the id of elements in the DOM to do it's thing.
When
it tries to do it's thing it finds the first element with the id
specified.

You need to specify each with a unique id.

One way would be to include a random string generation function in a
helper.  eg

def random_id
  chars = ("A".."Z") + ("a".."z") + ("0".."9")
(1..10).inject(String.new) { |str, i| str << chars[rand(chars.length)] }
end


 then in your partial
<% rand_id = random_id %>
<p name="#{rand_id}" id="#{rand_id}">
other stuff in here

I think this should work unless you need the id to relate to the actual
record in the db
E4c4a05719644c37f02dc5181de7c8c5?d=identicon&s=25 Seth Buntin (Guest)
on 2006-05-31 16:55
The weird thing is when I put <% rand_id = random_id %> in my partial
the rjs won't display my partial.  I can take it out and it works fine.



Daniel ----- wrote:
> The problem is that each of your <p> elements all have the same id.  The
> javascript works with the id of elements in the DOM to do it's thing.
> When
> it tries to do it's thing it finds the first element with the id
> specified.
>
> You need to specify each with a unique id.
>
> One way would be to include a random string generation function in a
> helper.  eg
>
> def random_id
>   chars = ("A".."Z") + ("a".."z") + ("0".."9")
> (1..10).inject(String.new) { |str, i| str << chars[rand(chars.length)] }
> end
>
>
>  then in your partial
> <% rand_id = random_id %>
> <p name="#{rand_id}" id="#{rand_id}">
> other stuff in here
>
> I think this should work unless you need the id to relate to the actual
> record in the db
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-31 17:37
(Received via mailing list)
Seth,

My bad..  the chars line should read

chars = ("A".."Z").to_a + ("a".."z").to_a + ("0".."9").to_a

or something similar
E4c4a05719644c37f02dc5181de7c8c5?d=identicon&s=25 Seth Buntin (Guest)
on 2006-05-31 18:04
Thanks Daniel.  That worked.

Seth

Daniel ----- wrote:
> Seth,
>
> My bad..  the chars line should read
>
> chars = ("A".."Z").to_a + ("a".."z").to_a + ("0".."9").to_a
>
> or something similar
This topic is locked and can not be replied to.