2

After some research as to how I should replace Prototype with jQuery I've ended up with this jquery-rails:

gem 'jquery-rails', '>= 1.0.3'
rails g jquery:install
rails s

I've included the :defaults files (which jquery-rails should have replacements for) but I'm getting errors when implementing an AJAX login script. Creating sessions and logging the user in goes well, but the code generates errors:

TypeError: Object function Element() { [native code] } has no method 'update' Element.update("sidebar", ":partial => 'layouts/user'");

Controller:

sign_in user
  respond_to do |format|
    format.js {
      render :update do |page|
        page.replace_html 'sidebar' , ":partial => 'layouts/user'"
      end 
    }

Form:

<nav id="sidebar">
    <%= form_for :session, :url => sessions_path, :remote => true do |f| %>
      <div>
        <%= f.label :email %><br />
        <%= f.text_field :email, :class => "span-4" %>
      </div>
      <div>
        <%= f.label :password %><br />
        <%= f.password_field :password, :class => "span-4" %>
      </div>
      <div>
        <%= f.submit "Sign in" %>
      </div>
    <% end %>
</nav>

If I'm not mistaken jquery-rails is supposed to be a drop-in replacement for Prototype. So I'm guessing I'm doing something wrong?

Edit:
Added the sidebar nav

Emil Ahlbäck
  • 6,085
  • 8
  • 39
  • 54

2 Answers2

6

You have to create a partial called XXX.js.erb, delete the one called XXX.js.jrs (if there is one) and inside of the first one, use JQuery (mostly its .html() method), for example:

=> controller

respond_to do |format|
  format.js { render :locals => { :foo => 'bar' } }
end

=> js partial (XXX.js.erb)

$('#element')
 .html("<%= "hello #{foo}" %> ");
yagooar
  • 15,959
  • 6
  • 20
  • 21
5

The problem is that page.replace_html is a prototype function. See this question for reference. Unobtrusive Javascript is your best bet.

Community
  • 1
  • 1
David
  • 7,310
  • 6
  • 41
  • 63
  • Ah. I thought jquery-rails would also replace those functions (and thus be a replacement for prototype). Thanks for the clarification! – Emil Ahlbäck May 28 '11 at 10:58