Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:46:33+00:00 2026-05-25T00:46:33+00:00

I’ve recently read that embedding ruby inside JavaScript is not a good idea. However,

  • 0

I’ve recently read that embedding ruby inside JavaScript is not a good idea.
However, in books such as David Heinemeier Hansson’s Agile Web Development with Rails, that’s exactly what it does.
If embedding ruby with JS is NOT a good a idea, then what’s the best practice for such case?
Given something as simple as this: (jQuery + ruby)

posts_controller

def create
  @post = Post.new(params[:post])
    respond_to do |format|
      if @post.save
        format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
        format.js #will use this response to process ajax
      else
        format.html { render :action => "new" }
      end
    end
end

create.js.erb

$tr = $('<tr>');
$td1 = $('<td>').text('<%= @post.title %>');  
$td2 = $('<td>').text('<%= @post.content %>');
$tr.append($td1, $td2);
$('table tbody').append($tr);

How should it be refactored to follow the “best practice” of not embedding ruby with JS?(if that’s the case)

I really need to be enlightened on this, and maybe get the concepts right because I have read that rails 3.1 will separate JS from Ruby completely through assets ?(Is this correct?)

Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:46:34+00:00Added an answer on May 25, 2026 at 12:46 am

    RJS templates were essentially the way things were done for a long time, which is why you still see such a prevalence of the technique in tutorials. That being said, as you’ve noticed, they’re on the way out, and with good reason.

    I’m sure there are many reasons why RJS templates are a very bad thing, but a big one is how tightly they couple your view JS to your view HTML. Many problems arise from this, a few being:

    1. Lack of flexibility.

      Using your bit of code as an example. What if you wanted to be able to create posts from a different view? And have a different effect? Maybe there’s no <table> on the page at all and you just want to pop up a "success" message? Etc.

      What if you just wanted a data representation of your posts, but your javascript templates were written to manipulate HTML?

      How do you handle all this in one create.js.erb, which is tightly coupled to, most likely, the posts new.html.erb template?

    2. Complexity.

      RJS templates operate in somewhat of a vacuum. Generated on server side, their execution is not bound to DOM events. This makes it tricky to do things like, say, update a <form> on a page after an object is being created, as you have no frame of reference to select the appropriate <form> in the JS template (e.g. <form id="new_post_123">). There are workarounds for this, but they’re more convoluted than they need to be.

      By binding to a form client-side and dealing with the result, this problem is eliminated. You don’t need to find the proper form to update after a response.

      With RJS you have no such binding, and you’re forced to use the known structure of the HTML to retrieve and manipulate the appropraite elements. One example of unnecessary complexity.


    Regarding your question:

    I really need to be enlightened on this, and maybe get the concepts right because I have read that rails 3.1 will separate JS from Ruby completely through assets ?(Is this correct?)

    This is essentially true. The asset pipeline, while cool, doesn’t offer anything brand new. Asset frameworks like Sprockets and Sass, and the workflows they enable, have been around for a while. The Rails 3.1 asset pipeline just brings them into standard practice. It’s more a matter of perspective, as DHH talked about in his recent RailsConf keynote. By bringing greater organization to these assets, js files in particular, it makes them feel more like first-class citizens, so to speak, deserving of as much attention as your back-end code. As opposed to the "junk drawer" feel of public/javascripts.


    As for the implementation:

    You might do it something like this (although untested and a bit simplified, e.g. in Rails 3 you might use responders for this rather than a respond_to block:

    # as you have, but returning the object as data which can be handled client-side,
    # rather than RJS generated by the server
    def create
      @post = Post.new(params[:post])
      respond_to do |format|
        if @post.save
          format.js { render :json => @post }
        else
          # ...
        end
      end
    end
    
    // Then in your client side JS, a handler for your remote form.
    $(function(){
      $('#your-create-form').bind('ajax:success', function(data, status, xhr) {
        // simply repeating what you had in the template, replacing the erb with
        // attributes from the returned json
        $tr = $('<tr>');
        $td1 = $('<td>').text(data.title);  
        $td2 = $('<td>').text(data.content);
        $tr.append($td1, $td2);
        $('table tbody').append($tr);
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.