Forum: Ruby on Rails drop_receiving_element question

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.
jeff.gray (Guest)
on 2005-12-09 21:56
(Received via mailing list)
Hi...

First I'd like to say that this mailing list is EXTREMELY helpful to me.
Thank all of you for answering questions.

I am hoping that someone can help me with this problem:

I have a draggable element being dropped on another element with this
code:



<%= drop_receiving_element  "element1",

      :accept => "item", :hoverclass => "item-active",

      :url => {:controller=> 'testcontroller',

      :action => 'test'}

        %>

I need to find out how to get the ID for this droppedOn element (in this
case, "element1" - but how to pass it to controller?)

And also the ID for the draggable element.

How do I pass these to the controller?

Is it possible to put javascript inside the :url parameter?  Everytimg I
try something like:

:url=>{:action=>'test',
:element1=>"document.getElementById('this.parentNode').id"}

It will fail with this.

Any ideas on how to pass these element IDs?

Thanks in advance.
thiessen (Guest)
on 2005-12-10 00:55
(Received via mailing list)
Gray, Jeffrey <jeff.gray@...> writes:
> I need to find
> out how to get the ID for this droppedOn element (in this case, â??element1â?
> â?? but how to pass it to controller?)

The DOM ID for the drop receiving element isn't automatically passed
back to the
controller.  However, there's nothing stopping you from passing your own
parameters back to the controller as part of the URL, for example:
  :url => { :controller => 'testcontroller',
            :action     => 'test',
            :where      => 'element1' }
When the "test" action runs, it can check params[:where] to see which
drop
receiving element is calling it.


> And also the ID
> for the draggable element.

That one's easy: the DOM ID of the draggable element that's dropped on
your drop
receiving element will automatically be included as the :id parameter in
the
method call--your controller just reads params[:id] to see what it is.


> Is it possible
> to put javascript inside the :url parameter?

No--at least, not without a lot of work; the approaches above should
make it
unnecessary, anyway.  You might, however, want some Javascript to be
executed,
however, when a droppable is dropped on your drop receiving element. For
that
you use one of the optional "callback" parameters to the
drop_receiving_element
helper, for example, in the :after, :loading, :complete, or other
parameters.

--Forrest
jeff.gray (Guest)
on 2005-12-10 02:50
(Received via mailing list)
Thanks for the help forrest.  You stated I could do "where =>
'element1'"
but how does Rails know it is a DOM object?
thiessen (Guest)
on 2005-12-10 03:02
(Received via mailing list)
Gray, Jeffrey <jeff.gray@...> writes:
>
> Thanks for the help forrest.  You stated I could do "where => 'element1'"
> but how does Rails know it is a DOM object?

It doesn't know--when you feed ":where => 'element1'" to the url helper,
all
it knows is that you want to send an additional parameter called
"where",
with value "element1" as part of the URL, so you'd get a URL that looks
something like:

   http://whatever.com/testcontroller/test?where=element1

It doesn't know "element1" is a DOM ID--it could be anything.

The trick to this is that *you* know the DOM ID, and when you write the
Rails
code to create the drop receiving element, *you* can specify that ID as
the
value for the "where" parameter.  If your web page has 3 different drop
receiving elements, you write a drop_receiving_element helper for each
one, and
give each one a different "where" value, so that when the AJAX requests
come in
to it, it can tell the difference between them.  This means that you
don't have
to use DOM IDs at all--you could use any unique identifiers that your
action can
tell apart.

--Forrest
jeff.gray (Guest)
on 2005-12-10 16:34
(Received via mailing list)
Thanks again Forrest.  However, I do not now the element ID...it is
being produced by a sql query.  But I know the parent id and child
id...so I need to use JS to get the node.  is this possible?
thiessen (Guest)
on 2005-12-10 19:18
(Received via mailing list)
Gray, Jeffrey <jeff.gray@...> writes:

>
>
> Thanks again Forrest.  However, I do not now the element ID...it is being
produced by a sql query.  But I know
> the parent id and child id...so I need to use JS to get the node.  is this
possible?

Well, it is generally possible, but can be tricky--too tricky to be
really
"Rails-like". One tool that's a big help with something like this is to
try
something in Rails, then pull the page in question up in your browser,
and tell
the browser to "View Source"--look at the HTML source for the page that
Rails
generated and see why the Javascript you inserted isn't quite coming
through
correctly, adjust, and try again. After doing this the first time I can
often
see what I'm doing wrong (usually something like missing quotes in the
right
place) and get it working on the next try. One thing that can make all
of this
hard is that Rails isn't expecting Javascript in certain places, so
sometimes it
will "helpfully" try to escape some of the strings containing your
Javascript so
that it WON'T be executed as Javascript, so you might have to go through
some
gyrations to get it to work.

Sorry I don't have time to try some of this out to give you a working
example--I'm a little busy today!

As I think about it, though, you might see if there isn't a different
way to
approach the problem; it sounds odd that the controller/view creating
your page
doesn't know the id of an element on the page that it itself is
rendering--after
all, even if it's coming from an SQL query, that query is still being
done in
your controller, so the controller still has access to the information.
From my
experience, the only time you need to use funny Javascript like this is
when you
 need to know the DOM ID of something in your user interface that the
user is
actively changing--for example, the ID of an item being dropped on
something--NOT to get the ID of a static fixture on the page, like a
drop
receiving element.  So, you might be able to do what you want using the
method I
describe above, but it isn't feeling right to me.

Good luck!

--Forrest
This topic is locked and can not be replied to.