Why JS.ERB is so bad? [closed] - ruby-on-rails

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I would like to have more information and concrete cases how bad is JS.ERB in a Rails application.
I have something in mind:
Lack of flexibility
Complexity
Mix Javascript + Ruby ( possible + HTML) code

The worst thing about this is that javascript is supposed to be client side language and building it server side seems at least awkward.
Apart from that, you will get a major performance hit if you are not able to precompile your assets - every request for javascript file in production server has to get through your rails application to render erb correctly. (If your .js.erb files depends on variable, request related values). If you avoid js.erb files and have .js files only, those files are completely static and can be served directly by the server without touching your application at all.
There are naturally couple of exceptions - I personally allow myself to use erbized javascript files to avoid hardcoding urls or other application constants. For me, the rule of thumb is - if erbization doesn't stop you from precompiling and will make your code cleaner and more resilient, just go for it.
Naturally there are some reasons not to do the above as well - in the ideal world I would love my server side application to be pure API consumed by another, pure static js application. Using erbized javascript is an extra obstacle from splitting your app in half when it grow too much.

js.erb is generation of javascript with ruby, right? To me this has an obvious "code smell", of code generation. Whenever someone resorts to using one (more familiar) language to generate another (less familiar) language, I would question the quality of the generated code. In this case a javascript guru would probably look a the generated code and be thinking "WTF?, why would someone do __ this way?". There are probably going to be a lot of things that work, but are not how an experienced javascript developer would do it. This can lead to performance issues, maintainability issues, and just plain bad code.
Just my opinion.

Warning - Highly Opinionated answer to follow. Moderators will probably delete!
I can't agree more, in fact I am a bit of a nut about this, and have been using react.rb to get all of our views into straight ruby, no JS, ERB, at all.
My specific reason beyond what you mention is this: The mental power it takes to constantly shift between different languages is huge, and largely taken for granted by developers. We just don't notice how much energy its taking. In otherwords we have become like frogs in the pot of water, and we keep making our programming lives more complex with all these languages and sublanguages (we were programming in 5 different languages in one site) and while you get used to it, once you stop, and stick to one language (ruby in our case) you just say whew!
While I have no specific study to prove this, I think its just a form of multitasking which is a known evil. i.e. https://www.psychologytoday.com/blog/brain-wise/201209/the-true-cost-multi-tasking

Related

Why learn Ruby on Rails [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
One of my college professors said that ruby on rails is used a lot for web, and I'm wondering how much Ruby on Rails is actually used vs JQuery, Node.js, PHP, etc. Also, what are the benefits?
You are mixing some stuff:
Ruby on Rails is a framework to create server side web applications using the Ruby language
jQuery is a client side JavaScript library that simplifies writing JavaScript web clients
Node.js is a server for the execution of server side JavaScript, thus providing a server version of JavaScript
PHP is a language popular for server side web application development
Thus: Ruby on Rails is a mature framework which offers a template engine, MVC architecture, a mapper between language objects and some relational database and a routing facility between URIs and controller.
Similiar designed frameworks exist for many programming languages / environments, e.g. Django for Python, or see Rails-inspired PHP frameworks in case of PHP.
About its popularity, see e.g. http://hotframeworks.com/
Benefits: IMHO it is a very elegant framework and as the plethora of inspired frameworks shows, has found many developers who like it.
The concepts and techniques learned here might also turn out to be useful when working with other modern frameworks.
And I should note as well, that there are web applications that need less features, e.g. see the Sinatra framework for a lighter alternative.
Also, what are the benefits?
There are a lot of things that websites have in common, e.g. html pages with forms, various javascript features, database interactions, security issues, logging in, etc. If you start from scratch, and try to program all those things yourself, it will be difficult and time consuming, and most likely your code will be full of exploitable security holes.
The other option is to use a web framework. Ruby on Rails is a web framework for the ruby programming language. All the various server side programming languages, such as ruby, python, php, perl, java, etc., have web frameworks(and usually many different frameworks to choose from!). A lot of smart people have come up with the best code for various things that websites need, and you get to use their code for free in your website.
The disadvantage of frameworks is that they are often large and complex, e.g. Ruby on Rails, Java Servlets+JSP, so it can take awhile to learn how to use them. Even then, you will probably not have a good grasp of their inner workings, so you are always sort of feeling around in the dark trying to get them to work the way you want them to. It's sort of like trying to push a large boulder which is at rest to another spot of your choosing: sometimes the boulder rolls cleanly into position, and other times the boulder seems to have a mind of its own and wanders off course.

using rails for a very thin application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've been working on a large website with Ruby on Rails for more than 2 years now and found it really convenient and easy to code a web application or service. Now I want to build a really thin web service: no DB, no front end 1-2 controllers with 2-3 actions each.
When I invoke rails new, it sort of "scaffolding" a large application structure, there is any way to get the default large structure replaced for a small service suitable structure with rails? (btw, If you think I don't need to use rails for such a project you're welcome to explain why. )
I was just going to upvote some comments, but we try not to answer in comments, so I'll try to synthesize the relevant points:
Rails' infrastructure is designed to accomodate a fairly complex app, and is probably overkill for the sort of simple app you're talking about building. This matters because Rails embraces Convention Over Configuration, and the conventions are geared toward larger apps.
Here's a couple of examples (not an exhaustive list):
Rails separates routes into their own file, with individual controller classes for interacting with each type of resource. If you have only a handful of pages in your app, this can be more structure than you need.
Although you can tell it not to, Rails assumes by default that you want to use ActiveRecord (and that you have a database for it to talk to). It also assumes you'll want model classes, and that there's both enough of them and they're complex enough that they need their own directory of individual files.
The Rails Asset Pipeline is great for dealing with lots of complicated assets that require extensive preprocessing. It's also kind of a pain in the ass.
There are a number of Ruby-based microframeworks that are likely better suited to the sort of app you describe. Sinatra is probably the most widely used. It makes few assumptions and is consequently lightweight, allowing you to establish whatever design patterns you want in your app. There are also bunch of others that might be worth considering.

Front-end + Back-end development [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've been really getting into Ruby on Rails. I want make great apps that don't just function well but look really nice as well, and I'm worried I might not pick the right front-end language to compliment RoR development. My question is how do you simultaneously prepare for both back-end and front-end? Summer is coming up and I'm already enrolled in a few RoR classes, but am trying to choose some great front-end courses as well. Appreciate any guidance
Sam --
TLDR; Know HTML and CSS like the back of your hand. Spend time learning intermediate and advanced javascript. Its libraries are powerful, its frameworks provide a rich client experience, and its not going away anytime soon.
I've been a RoR developer for two years. When I was first learning Ruby, and coding principles in general, its common to want to learn and learn all kinds of different stuff. While it's tempting, I would suggest the opposite -> Hyperfocus.
Rails is a robust framework, spend some time with it. I will agree that Rails views are very bare bones and I do not like the way they organize javascript and stylesheet logic. It is very loose. But, unless you're building a huge app, organizing .js and .css files isn't super important.
As far as looks go, html and css are the backbone. There's literally a javascript library for everything you could want to do from a UI/UX perspective, so focus on intermediate/advanced js. That will make opening doors to robust client side js frameworks like angular, ember, and backbone much easier down the road using Rails as simply an API.
Lastly, one of the hardest things to find in the industry is a great designer who can also code, or a great software engineer who can also design. If you really care about UI/UX get some books on fundamental web design principles; they go a long way.
Well, I encourage you to first code a few apps with the default rails stack and after that start to poke other front end options with rails as back end api.
Stay comfortable with rails first, has a lot of good tools and is enough for most of the apps you may want to build, mainly on the first stage of your learning path.

Why use Node.js if I don't require real-time functionality? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm considering using Node.js with a framework such as express, meteor, or sails (a directory with social features such as sharing, messaging and uploading media). I don't have any features planned that explicitly require real-time functionality, so does it make sense to use Node.js anyway instead of Rails?
There's so much buzz around Node.js that I am tempted to use it just so that I don't get left behind.
As DHH wisely noticed regarding Node vs Rails, "everything can be used instead of everything else". That's somewhat true in a sense that, for example, a site in Rails with promptly set up caching can be as fast as one written in Node.js.
Besides, Node is not necessarily about real-time. It's more about being able to handle many light (in terms of processing time needed) requests. If you expect high level of concurrency (I mean, really, expect, not just are dreaming of it) and every request is supposed to be relatively small, then you could consider using Node, just because handling bigger load (up to some point) will require less work with Node.
Bottom line, use what you are good at. Unless you want to try something new. And Node.js is definitely worth trying.
You're question does lack quite a bit of context.
This question all depends on the context.
If this is contract work or something you want to make money with in the near future and you're not sufficiently skilled with any of the mentioned nodejs frameworks.
Then I would recommend you use whatever you're already good at.
If this is a private project for fun or any other non serious purpose.
Then I would seriously recommend you to try one of mentioned nodejs frameworks.
In my opinion nodejs is currently the cutting edge web technology. As a developer
you should always try to stay on the cutting edge. That way when you learn how nodejs can be used you might find ways to use those things in your professional environment.
I've lately been using meteor a lot and I can highly recommend it, once you get the hang of it you can do truly amazing things that you could never even imagine doing(in a reasonable timespan) in a classic php project.
Also according to some meteor will replace RoR alltogether blog
Aside from real-time, main plus I've seen is having same team being able to develop JavaScript client code and server side code for UI web applications. "Code sharing" between client and server seems a pipe dream to me, but same language is really nice.

Web development for a Computer Scientist [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have BS in Computer Science, and thus have experience developing software that runs at the command line or with a basic GUI. However, I have no experience making real, functional, websites. It has become apparent to me that I need to expand my skills to encompass web development. I have been using Ruby to develop applications, but I am aware that it is quite popular for web development. I want to use my skills as a programmer to assist me in developing a personal website for a band.
I have experience with HTML, but very little with CSS. I want to leverage my skills with programming languages to create a website containing pictures, audio clips, a dynamic calendar, a scheduling request tool, and other features common to band websites.
What kind of resources are available for a competent desktop programmer to learn the entire process for developing a website? Is it best to use free CSS templates and WordPress as a foundation for my site or make it from scratch? Should I use GUI tools or write it all in Vim/Emacs? Is Ruby on Rails sufficient for my personal website, or should I consider a more mature development platform?
My main goal for this project is to come up to speed on current web design technology, and actually understand the entire process for building a website.
I think one really important thing to understand in web development is HTTP. HTML and CSS are important, but I think it's more critical to understand the stateless nature of the web, and how each of the HTTP verbs work, and what they can/can't do.
http://www.freeprogrammingresources.com/http.html
A good tool for seeing how HTTP works is Fiddler.
If it's as much a learning exercise as anything then take an iterative approach. Build revise. Build revise. My (very) rough guideline below:
Client
Start with the structure of a website and concentrate on the client.
Use notepad and build a bunch of static pages for your band. i.e. Hand code initially. Try to build all your pages with CSS. No table markup. Then play around with some Javascript to bring things to life. (Navigational menu\ Calendar selections\etc). Learn about how to import and link to Javascript and CSS files.... and how these files are treated re:caching etc.
Try to learn up to the limits of what you can do on the client (generally). Factor in the nuances of 3-4 browsers (Firefox/IE6/IE8/Chrome) re:DOM and client side eventing.
Server
Then start looking for what you might want to change across pages/sessions. i.e. what needs to be manipulated server side. And pick a server side technology.
Start with basic post-back processing. Forget databases at this point. Learn how your framework of choice maintains state..... not just the name of the technology but the real nuts and bolts of it. One of your single greatest assets as a web developer is understanding the state model(s) of the technology you're using.
Then go for a deep dive on the web server technology of choice (and in general). Understand the full request pipeline from client to server and back. This will teach you forms, http and its verbs, web server, filters and modules, server to framework hand off, page and control life cycles, back to the client.
Now start working on dynamic content injection and the like. How to make and use reusable components in your web pages.
Databases, caching, performance and diagnostics.
Then get into into all the fun stuff like ajax, etc. Replace your javascript with jQuery, etc.
Then you got the whole Webservices\XML\JSON\etc side of things to discover.
Resources
Well the web obviously. For client side stuff, going to the sites of companies who make third party web controls can be quite interesting. Asking how the hell they did that? Viewsource is your friend. Look at how they structure and build their pages. Pick a couple of good web designer sites, and you find a plethora of rants about browser wars etc that will give you good (under the hood) info.
Once you hit server side, I'd go for white paper type learning from your vendor of choice for your technologies.i.e. webservers/frameworks/etc. Again find a 3rd party howto/evangelist site (I used to use a lot of "4 guys from Rolla" for example) that will demonstrate how to do various things. Language learning is ongoing. Basically just do the best you can till you find a better way.... and always be on the lookout for a better way.
You really need to understand html, forms and css to get anywhere. I say forms as this will give you the round trip needed to understanding the stateless nature of web dev.
To further labour the point, I have interviewed many people who think you can only have one form on a page and can only have one submit button per form. This is all down to a lack of foundation knowledge.
So for that I'd recommend starting with htmldog.com.
After that, a lot of web development is done with frameworks. Gone are the days where you do it yourself (well mostly) but my above point still stands. You do need to be able to peep under the hood with some confidence.
I've been doing web development for 12 years and started out with Perl on Solaris and Linux. Since then I've also done Java and more recently ASP.NET. However, I'm slowly falling for Django in my private projects.
What I've found over the years is that the inherent problems - cookies, javascript, presentation, state, authentication are all the same but just handled differently. So ultimately its down to you and your language preference. Plus a little of enlightened self interest when it comes to potential employment.
Programming aside, you should also become familiar with web servers (Apache and IIS spring to mind), Http codes and headers, Mime types and encoding and FTP. As well as Javascript (mentioned already), plugins, browser platforms and good development practises such as using Firebug, Fiddler and so on. It also wouldn't hurt to have a good idea of the image formats available, image optimisation, CSS sprites, content compression, caching and the like.
All depends on where you want to start!
For a newbie, I'd pick Django and (obviously) Python. Good, clean language with cheap startup options, low cost IDEs (ie free) and hosting your sites is very affordable.
But that's just a subjective opinion.
If your goal is to
My main goal for this project is to
come up to speed on current web design
technology, and actually understand
the entire process for building a
website.
Then start from scratch in Ruby, PHP, Java, ASP.NET, etc...
When you run into a design problem or just want to know how others have approached something, then look at the frameworks.
Once your up to speed, and your website is starting to grow, then segway into a framework, to get up to speed on the frameworks.
I agree with John on this one.
As you know from your own experience in pursuing your BSc, understanding the basics of any language is what makes you even more capable in expanding that knowledge or specializing.
With that in mind, it would be best to understand the basics of HTML and CSS.
Understanding the syntax and overall language will help in the future when you want to pursue large projects using frameworks like Django and Rails. The basics will also especially help with tweaking CMS' like Wordpress to be more customizable to your needs.
One thing in particular that I'd like to mention is that web programming, like many other forms of programming has its own special structure and "proper" way of doing things.http://www.w3.org is a great way to ensure that your work is passing general web design standards, most sites don't follow this because it is tedious, but from a learning perspective it ensures that you get a nice strong foundation.
www.w3schools.com is also a great resource for detailed help on web programming. Lastly, I like colourful code, so I like using basic text editors such as notepad++ or notepad2 or gedit to do my web coding. GUIs like dreamweaver may tend to fill up your code with extra junk and spaces, so I don't recommend them, but they are still great tools.
Don't bother with Rails yet -- write CGI scripts in Ruby. It will be very similar to what you have done for class.
After you have about thirty of those under your belt, you'll know what you want out of a web framework.
I'm a Computer Scientist and a web programmer and I would suggest you learn both HTTP and CGI:
CGI Made Really Easy
HTTP Made Really Easy
As the titles of the above tutorials claim, they made the concepts "really easy" for me.
Once you've got CGI and HTTP down pat, I'd suggest checking out following sites that provide a wealth of articles and references for web programming:
webmonkey
w3schools
Mozilla Developer Center
Assuming you want to concentrate on writing web apps, then Perl, PHP, Python and Ruby are all fine choices (I myself use Perl predominantly) and I'd suggest doing some research into the popular web frameworks available for each language.
Most importantly, pick something simple as your first web app, e.g. a form and a page that shows the results of submitting that form. Some good examples (using Perl's CGI module) can be found here:
CGI.pm - a Perl5 CGI Library -- see the first set of links on this page.
When you want to start writing web apps that use a database, read up on SQL and popular libraries/modules in your chosen language for database manipulation, especially ORM (Object-Relational-Mapping) interfaces that allow you to deal with records in an Object-Oriented fashion.
Good luck with it! Being a web programmer is fun because your audience is teh intarwebz! :)
If you are starting from scratch as per John MacIntyre's suggestion, you may lean towards PHP. With all of its shortcomings, it does have one really good user manual. It is also easy to get started with and is installed on pretty much every host and goes well with Apache.
Also, w3 schools is good to begin learning about CSS and XHTML but don't forget to check out the specs at W3C.
Also, please read this Stack Overflow question & answers.
For what you're describing, Rails or Django might be slight overkill but it wouldn't hurt to learn them. Django, in particular, might be good because of the notion of a project containing multiple apps (e.g. calendar).
Whether you use a framework or write everything yourself, though, you'll need to know HTML and CSS. CSS is extremely simple if you have a BS in CS...you could read a tutorial and know it in five minutes.

Resources