Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much JavaScript do you let Rails generate?

Ruby on Rails has a lot of ways to generate JavaScript. Particularly when it comes to Ajax. Unfortunately, there are a few problems that I often see with the JavaScript that it generates.

  • Rails typically uses inline event handling.

    <a onclick="somejavascript(); return false;" />
    

    This is generally frowned upon, as it's mixing behavior in with the XHTML.

  • The generated JavaScript also relies heavily on Prototype. Personally, I prefer jQuery.

  • In my experience, the attitude with a lot of Rails developers has been to write as much of the code in Ruby as possible. The final step is to generate some very procedural and repetitive JavaScript. Often, this code ends up being very inflexible and difficult to debug.

So, my question is: how much JavaScript do you write manually for your projects and how much of it is generated server-side with Rails/Ruby? Or is there a happy medium where you get the benefits of both? With a subquestion: if you write a lot of the JavaScript manually, what techniques do you use to fit it into the MVC model?

like image 419
Matt Ephraim Avatar asked Oct 21 '08 22:10

Matt Ephraim


People also ask

Does rails use JavaScript?

Rails uses a technique called "Unobtrusive JavaScript" to handle attaching JavaScript to the DOM. This is generally considered to be a best-practice within the frontend community, but you may occasionally read tutorials that demonstrate other ways. When clicked, the link background will become red.

Do you need to know JavaScript for Ruby on Rails?

Ruby on Rails: Full Tech Stack In the process of learning Ruby on Rails, you will end up learning a little about JavaScript, HTML/CSS, and Ruby programming languages. However, if you were to start learning any of these programming languages, you will not learn anything about Ruby on Rails.

Is Ruby on Rails better than JS?

Performance – Ruby on Rails has medium level performance as it doesn't support asynchronous programming. It also has a high CPU processing time, while Javascript has greater performance with asynchronous support and event driven single threaded architecture.

Is RoR front end?

Disclaimer: RoR is generally used for backend and JavaScript for frontend development. We want to dive deeper into the peculiarities of these two technologies because they are often used within one tech stack.


2 Answers

If you prefer jQuery you can use the jQuery on Rails Project. A drop in to replace Prototype with jQuery.

Some of what Rails does with Javascript generation is good and some is bad. In the bad instances, write it yourself and keep it unobtrusive. At any given time you're uncomfortable with the Javascript Rails generates, you can go ahead and write it yourself.

And be sure to check out this great intro to unobtrusive Javascript that was done with Rails in mind.

like image 176
mwilliams Avatar answered Sep 19 '22 12:09

mwilliams


I used to work in Symfony (a Rails clone) and at first, we used a lot of Javascript helpers. Client requirements led us (me!) to have to write a lot of code the helpers just couldn't generate. I eventually came to the conclusion that I prefer not to use helpers at all.

Progressive enhancement is the way to go, in my opinion. Generate standards-friendly HTML that works without JavaScript enabled, then pile on the fancy functionality on document ready.

By the way, I've also switched from Prototype to jQuery and have no desire to switch back! In my opinion, jQuery is better suited to progressive enhancement.

like image 22
Andrew Hedges Avatar answered Sep 20 '22 12:09

Andrew Hedges