Rails 3 and Rjs

Rails 3 and RJS

here is the screencast http://railscasts.com/episodes/205-unobtrusive-javascript

UPDATE April 2011: RJS is being extracted for Rails 3.1

prototype-rails is going to be a gem when Rails 3.1 is out

Applications using RJS have to add this line to their Gemfile while working against Rails master before the 3.1 release:

gem 'prototype-rails', :git => 'git://github.com/rails/prototype-rails.git'

more info on ruby on rails's could be found in this article

prototype-rails on github

Rails 3: RJS vs JavaScript

In rails you should use both approaches for different tasks. So do I. When I would like to execute ajax request, I use RJS because it simplifies the request and allows to write more powerful and flexible respond within .rjs file with a much less code. For example it is possible to use partials in it and to get access to ruby variables. When I would like to make a page more dynamic, I use plain javascript including it with javascript_include_tag.

UPD.
This approach works in rails 3 the same way. It's possible to use it with JQuery and HAML. UJS in Rails 3 even simplifies it. The only thing you should do is to use jquery-ujs if you prefer Jquery over Prototype and proper file naming like create.js.haml.
For example it possible to use server-side validation in AJAX requests with this approach.

Rails 3.2.2 not executing rjs

It looks like rjs has been removed as the default as of Rails 3.1. You could get it back by installing the prototype-rails gem, but I think you should just use jQuery, which is the new default.

As for your code, the reason it's not working is that it's an rjs template being interpreted as .js.erb, and this is likely just producing invalid JavaScript (you should see errors in the JavaScript console in your browser). An rjs template used to set the page variable for you, and you would write Ruby code using it to manipulate your page. In a .js.erb template, it works more like in your .html.erb views. You write actual JavaScript, with Ruby embedded using <% %> tags. So the code in create.js.erb should look something like this:

 alert('NO PROBLEM HERE');
$('#cart').html("<%= escape_javascript(render(@cart)) %>");

Rails 3 Link_to :remote is not triggering RJS

You need to explicitly tell Rails that you want the js format:

= link_to 'Add a zone →', new_zone_path(:format => :js), :remote=>true

As a way of explanation: You have to specify the .js extension because Rails doesn't discriminate. In many cases, you might want to fetch html or json with Ajax--not just javascript. Rails will let you fetch whatever content in whatever format, which is why you have to specify it.

Convert rjs to js.erb in rails 3.1

I found a solution.

We need to use Element.update in place of page.replace_html.

The new code looks like:

Element.update (
"overlay" ,
"<%= escape_javascript(render(
:partial => @path_resolver.resolve_template(
:tmpl_name => '../shared/show_popup',
:partial => true,

:object => @content_data
))) %>"
)

Rails3, JQuery and RJS (UJS)

Try to "live"

$('#choo').live('ajax:success', function(){
alert("Success!");
});

or this:

$('a[data-remote],input[data-remote]').live('ajax:success', function() {
alert('success');
});

Rails 3 remote link displays RJS code on page

please include csrf_meta_tag helper in the header..



Related Topics



Leave a reply



Submit