Forum: Ruby on Rails RoR 1.1 RJS problem

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.
Gerald A. (Guest)
on 2006-03-31 18:39
I've been playing with the new RJS stuff in RoR1.1, and I have to say
that I'm am extremely impressed, NICE! work guys.  However, I seem to be
having a bit of a problem.  I promise, I've googled the hell out of it
and haven't found an answer.

Set up:  Putting together a little proof of concept app.  Display a form
(index.rhtml) that just has a button to call the next action
(show_admin_conf).  Have created a show_admin_conf.rjs and the
show_admin_conf method is actually empty.  When it goes to the
show_admin_conf.rjs I'm just asking it to hide a <LI> tag with a given
id.  What I get is the <LI> tag updated AND the result of the RJS in the
content area where I'm trying to update (not related to <LI> tag) Should
be pulling up a new partial in show_admin_conf.rhtml.

the controller code

class AbeProto1Controller < ApplicationController


  def index
  end

  def show_conf_admin
  end

  def conf_admin
    render( :partial => "conf_net")
  end
end
*******************************************************************************
The index.rhtml:

<%=
javascript_tag("document.getElementById('index').className='selected';")
%>

<div id="main_title">
        <%= $PROJECT_NAME %> Configuration Wizard
</div> <!-- main_title -->

<div id="main_description">
        This wizard will walk you through configuring your <%=
$PROJECT_NAME %>
        system.  Click the Begin button to start the process.
</div> <!-- main_description -->

<%= form_remote_tag(:url => { :action => "show_conf_admin" }, :update =>
"main") %>

        <div id="main_content">
         -no content-
        </div> <!-- main_content -->

        <div id="main_options">
                Press Begin to start.

                <div id="main_buttons">
                        <%= submit_tag("Begin!") %>
                </div> <!--  main_buttons -->

        </div> <!-- main_options -->

<%= end_form_tag %>
********************************************************************************
the show_admin_conf.rjs (in same directory as my rhtml files)

page['index'].hide
puts "ONE"
render(:partial => 'conf_admin', :update => 'main')
puts "TWO"
********************************************************************************

And, finally, the _conf_admin.rhtml partial:

<%=
javascript_tag("document.getElementById('conf_admin').className='selected';")
%>

<div id="main_title">
        <%= $PROJECT_NAME %>: Configure Administrator
</div> <!-- main_title -->

<div id="main_description">
        Complete the following form to configure your administrative
user.  This user
        will have access to all portions of the system.  Please choose a
strong
        password and provide an email address that is monitored by an
administrator.
</div> <!-- main_description -->

<%= form_remote_tag(:url => {   :action => "conf_admin" },

                                        :update => "main") %>

        <div id="main_content">
         -no content-
        </div> <!-- main_content -->

        <div id="main_options">
                Press Next to continue.

                <div id="main_buttons">
                        <%= submit_tag("Next") %>
                </div> <!--  main_buttons -->

        </div> <!-- main_options -->

<%= end_form_tag %>
********************************************************************************

The div id "main" defined in my template (not really interesting, so not
included) is where the index.rhtml and _conf_admin.rhtml are supposed to
render.


Ok, now, here's what I'm getting, and ALL I'm getting in my "main" div:

$("index").hide();

Which is correct. . .sort of.  It DOES hide the index object correctly,
but displays the above in the div and does not show my partical in the
"main" div.

Been working on this for about 24 hours, most of that spent googling and
reading over everybody's examples (again, and again, and again : b).

Any help would be GREATLY appreciated.  I know something silly is going
on here, but for the life of me I can't figure out what.

Thanks!

Gerald
Gerald A. (Guest)
on 2006-03-31 18:44
Umm, I should note:  I am a Ruby/RoR newbie, have just finished the
pragmatic agile development with rails book (with the pickaxe handy for
reference) and am just trying to put that stuff into practice.  If you
guys see something that I'm screwing up that's not related, or something
that's not "idiomatic" I will gratefully take any advice.

G
Jonathan V. (Guest)
on 2006-03-31 18:50
(Received via mailing list)
>
> <%= form_remote_tag(:url => { :action => "show_conf_admin" }, :update =>
> "main") %>


Remove :update => "main", use RJS to update whatever bits you want.


> page['index'].hide
> puts "ONE"
> render(:partial => 'conf_admin', :update => 'main')
> puts "TWO"
>

To replace the contents of the div 'main' with the conf_admin partial,
use:

  page.replace_html 'main', :partial => 'conf_admin'

You can also use Element.addClassName (from Prototype) to add that class
name:

  page['conf_admin'].add_class_name('selected')  # Should work, never
used
it though...

Your example seems a bit convoluted, it could be cleaned up a lot.

-Jonathan.
Marcel Molina Jr. (Guest)
on 2006-03-31 18:57
(Received via mailing list)
On Fri, Mar 31, 2006 at 04:39:43PM +0200, Gerald A. wrote:
> ********************************************************************************
> the show_admin_conf.rjs (in same directory as my rhtml files)
>
> page['index'].hide
> puts "ONE"
> render(:partial => 'conf_admin', :update => 'main')
> puts "TWO"
> ********************************************************************************

At least one of your problems is that you aren't meant to use render
from an
rjs template. Instead you'll use, for example, replace_html on the page
object.

For example:

page.replace_html 'main', :partial => 'conf_admin'

marcel
Gerald A. (Guest)
on 2006-03-31 19:21
That was it guys, thank you very much!  It never even crossed my mind to
revisit the :update in my form action (it was originally done as a
non-rjs test).  I had played with page.replace_html in an earlier
iteration, but with the rjs and :update set up as it was, it didn't work
exactly right ; )


Jonathan:  Like I said above, I'm learning.  If you have any specific
criticism I'm interested in hearing it.  I know that the example is
contrived, but don't understand where it's convoluted.

Finally, there's one more little related problem.  The
page['index'].hide line was a test, the actual goal is to set the class
of the <li id='index' class='selected'>blah</ul> to be 'complete'.  I'm
trying to use page['index'].assign('class','complete') but that's not
working.  Am I on the right path there?

Thanks again guys, really blown away with the rapid reponse ; )
Gerald A. (Guest)
on 2006-03-31 19:22
> page['index'].hide line was a test, the actual goal is to set the class
> of the <li id='index' class='selected'>blah</ul> to be 'complete'.  I'm

grr, sorry, typo. . .</li>
This topic is locked and can not be replied to.