Bad practices in Ruby on Rails [closed] - ruby-on-rails

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I'm looking for some examples of bad practices in Ruby on Rails, for a presentation on to what not to do.
My biggest on is to use update_attribute on a model after_save hook.
Object.update_attribute(:only_one_field, "Some Value")
As this is a very open ended question, I will wait a week or 2 and select as answer the most voted answer.
Have fun!

Too much mass-assignment without using attr_protected
Use of too many plugins - There are so many gems and Rails has sooo many plugins available for use in your applications. However, when you use a gem or a plugin, you rarely understand how your code is operating (unless you actually look at the source, which most people never do). This is a HUGE problem. You don't know how to debug code properly, plugins and gems clash with one another, security becomes a major concern, etc. For that reason, I always recommend writing all your own code. Sure, Devise is nice for authentication, but can you tell me exactly how it works and what queries are run? Do you have control over optimization? (I'm not picking on Devise, just showing a clear example that many RoR developers are familiar with)/
Keeping unwanted pages/actions - so many Rails developers use scaffolding (because it is nice), but then they don't bother to remove unwanted actions. It is as simple as adding :only => [] or :except => [] in your routes file, but most people never do! I don't know how many Rails sites that have been hacked or damaged because people didn't restrict the delete action
Trying to go against Ruby - developers who come from another language often have difficulties with the "Ruby-way." One of the most notable examples is having non-incrementing or non-integer primary keys.
Too much controller, not enough model - Rails had a "Fat Model, Skinny Controller" principle that all too many developers break.
Violations of MVC - accessing params in Models, trying to hack things into controllers, etc.
Not changing the default Rails unique session token (which is not actually random)
Writing sloppy code - Ruby has this great way of making code look readable. If you come from Java or PHP or even Python, your code is just plain ugly until you learn Ruby
Saying that Rails "is a language" or "I code in Rails" - absolutely, positively jerks me the wrong way when I hear someone say "I code in Rails" or "Rails is my favorite language", etc. RAILS IS NOT A LANGUAGE. Rails is a framework built on Ruby. This isn't related to security or the like, but you'll really irritate a LOT of RUBY developers if you start saying that RAILS is a language. It's a framework.
Comparing PHP and Rails - don't do it. Again, PHP is a language, Rails is a framework. Comparing them is unfair. (You can compare Ruby and PHP OR Rails and CodeIgnitor or CakePHP, etc)
Not properly catching errors - if it can go wrong, someone, assume it will, and plan ahead
Failing to optimize queries - this absolutely kills me. Rails doesn't force you to know SQL like PHP did (before ORM's like Doctrine), so Rails apps tend to be SLLLLOOOOWWW unless the developer is actually aware that you can optimize a query (joins say what??)
Using too many generators - you should be able to create a class (controller, model, test, view) without the use of a generator.
Using Rails for a large-scale system - yeah, most of you aren't going to like this, but ask Twitter and GitHub what happens if you build your front and backend in Rails... Let's just say Twitter uses a custom Java backend now...
Have a freaking clue - I get so annoyed because people don't actually know how a has_many relationship works (just one of 21914232 examples of dumb Rails developers)!
Not commenting code
Relying on Rails instead of SQL or DOM (javascript/html) to perform functions

IIRC, obie, or someone, in railsconf 2009/8 did a presentation on this already. There is never a reason to plagiarize but you could find inspiration there too if needed.

There are alot of "good practices" located at http://rails-bestpractices.com/ you way wish to look at some of the examples they have posts with before/bad samples of code.

Related

Does Ruby on Rails affect how a web page looks?

Most of the time, whenever I hit a website that looks "bubbly" in nature, and all prettified in those pastel-like colors, I think to myself, "This was probably done with Rails." And, lo and behold, after some digging into the site's information pages I discover this is actually true. So, I pose the question, not knowing much about Rails but enough about Django to understand how the database stuff works:
Does RoR have any display-specific qualities that affect how a web page looks? Or do all RoR devs naturally use the same Adobe tools to make everything look so ubiquitous?
Ruby on Rails is a server side technology, so it doesn't lend any specific quality to the user visible design. That said, it is a "trendy" technology so people who are likely to write their back-end code with RoR are likely to choose a particular "Web 2.0" style for their views.
As a Ruby on Rails developer, I can tell you that most Ruby on Rails developers are passionate about their work and we pay a lot of attention to details when building websites as much backend as front end. Its not just a trend, its a way of thinking and working.
No, it hasn't any display-specific qualities.
The theory is that RoR makes that backend stuff easier, so more time can, and apparently is, spent on the front end stuff.
Its all done with Mirrors. And CSS. :)
Rails is a very popular Web framework, it's just be coincidence that all the ones you've looked at have been rails apps.
What kind of sites have you been looking at to draw this hypothesis?
that's a funny question with a funny description :) ... bubbly!
As a madman, I develop with RoR, it's kind of rule in our area. We learn madness from the beginning, as a result of http://railsforzombies.org...
May wise men follow a wise path!
Short Answer: NO
However...
As a Rails developer I can say that due to the Agile nature of Rails and the speed in which you can develop web applications with Rails I do find myself having more time freed up on a project to spend polishing the user interface. I believe this may be a reason you often see more polished looking Rails sites.
So in my mind I believe your choice of framework can have a direct correlation to the end product that is produced!
Rails does add some stuff to the front end. Like to every html form, it will add a hidden input element authenticity_token.
You can also tell because rails URLs and form actions will never end with suffixes like .aspx or .php or .html or .jsp, and they won't usually append ?query=book&encoding=utf8 like you see on google. And they won't usually have superlong crufties like you see on amazon (eg http://www.amazon.com/Agile-Web-Development-Rails-Ruby/dp/1934356549/ref=sr_1_1?ie=UTF8&qid=1297922135&sr=8-1). Instead Rails prefers simple routing URLs. If amazon were written in rails, you might instead expect amazon.com/books/Agile-Web-Development-Rails-Ruby
So there are ways to spot a Rails app. I expect other web frameworks, especially the ones that emulate rails, would duplicate some or all of these features, so this isn't a sure-fire method, but it helps.

Would Ruby on Rails suit my work..?

I wanted to make a web site with the following basic features- (1)User registration for buyers and sellers. (2)profile pages (3)A buyer should be able to post work and should get profile links of the corresponding seller who has expertise in that work.
As time progresses i would want to add more features to the site.The freelancer sites where user can post jobs and get bids is the best example of the work.
(1)I want my code to be maintainable as i woud be adding features later on. (2)It should be quick to develop. (3)Resources should be available(not the entire thing, atleast in bits and pieces) for the above requirements and should not be tough to find for future enhancements. (4)Design should be decoupled from the buisness logic as i would outsource the design work.
I was thinking of Ruby on Rails for this work as i have experience in the MVC model and RoR looks cool.I am from the mobility domain so i don't know whether RoR will suit my work
Would RoR suit this purpose.If yes where can i find the resources to the above mentioned requirements.
Thanks
Ruby on Rails would be ideal for this type of website.
Check out some of these resources for info on how to use Ruby on Rails:
http://guides.rubyonrails.org/
http://railscasts.com/
http://www.railsforum.com/
I noticed you are already aware of TeachMeToCode, but there is a tag there for all the Rails 3 tutorials, with some blog tutorials and what looks like the beginning of a series on how to build a del.icio.us clone. Since they are in Rails 3, they would be well worth checking out:
http://teachmetocode.com/screencasts/tag/rails-3/
One of the best tutorials:
http://railsforzombies.org
It will let you have your own point of view quickly.
It depends on with which languages you have experience. Any good MVC framework will do the job just fine but if you like Ruby syntax RoR is definitely a good framework to develop this kind of application.

How to document a rails application?

I just started to document a rails application. I know this is actually done by rdoc, so I followed some rdoc guides regarding syntax and so on, but I got stuck when I tried to describe attributes of models, validations and the relationship between models, mostly because these things are part of ActiveRecord. So I wonder if there is some guide or a good practice regarding how to document a rails application or if there is something I'm missing?
I know that I could put all of this in the class description, but I wonder if there is a way more closely tied to the declaration itself (has_many, validates_presence_of, etc.) and what about the attributes?
I personally prefer YARD - http://yardoc.org , as it does a better job in documenting IMHO.
I don't know if there's a specific handler for Rails available, but it's quite easy to write one - http://yardoc.org/guides/extending-yard/writing-handlers.html
A good example might be the attribute handler - part of the yard gem:
lib/yard/handlers/ruby/attribute_handler.rb
Remember your tests are part of the documentation (for developers), particularly if you are using Cucumber where scenarios are easy to read. If you keep your methods very short and there is a test method with a descriptive name e.g. "should set the users name" I find I typically don't need comments on the method.
Validations or other parts of Rails I would not document. Part of being a Rails developer is understanding how these work, I think it is a fair assumption that another maintainer of your code reading it down the road will know validations, or other things built in to Rails. By that same logic, if you can use features of the framework or happy paths (not deviate much) with [documented] third party code, a lot of the documentation will be written for you.

Does a "vertical" framework for RoR make sense?

I have been spending some time creating what I called a framework.
It was aimed at the creation of quiz likes games. It was supposed to have two players syncronized and present them a question to solve, etc.
I would like it to be fully customizable so I tried to develop components that can be put in or out of the pages. In the end the elements became slim ruby methods to add a whole bunch of Javascript and CSS to the pages.
Still the Javascript needs to connect to Ruby so methods supporting it are created but they will only be present when the component is present. Some components depend on one another making everything overly complex.
And after this attempt I wonder, is there is not a better and easier way to make a framework aimed to one kind of application on RoR? Or is the concept flawed or RoR in some way flawed?
Ruby on Rails is a framework on its own accord and is "opinionated software". This means that the writers of Rails looked at what would make most sense for creating a web application to them. Many people support the original developers views and so use Rails for their projects as well.
I think your concept of creating a quiz is a good one, but first you need to understand the rails stack. Depending on what you need exactly, you can create either an engine, plugin or whatever.
What I have seen a lot is that you specify what you need in your controller. (How you do that is up to you). All that information is stored in a class variable and transferred to the view where you can render everything you need with some helpers. The hard part is making it all generic enough to be reusable.
But, maybe Rails isn't the right tool for you. Maybe you need something more lightweight like Merb or even Sinatra.
There is no 'flaw' in Rails. Rails is not the 10**1000-in-one tool Java is. It's a framework that tries to do one way very good in a particular way. I think Rails can be the right tool for you, but you need to be skilled enough to wield the tool :)

How: Ruby on Rails to build a basic site [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm a compsci student who wants to get learn a little about web development -- I learn best by doing. I know basic html/css/php/javascript/xml, but since Ruby is one of my favourite scripting languages, I figured I'd learn Ruby on Rails.
I'd like to build a basic website for a friend's club at school that just provides information about their organization and services they offer, and have an admin panel on it that contains a very basic inventory system (item, number in inventory, cost -- that's it) in order to learn Ruby on Rails. I'll be hosting it on a computer on campus -- so I don't have to worry about hosting.
This might sound a little silly, but as someone who's never built a website themselves, I was wondering how exactly one goes about it with rails -- like, how do I make a basic layout for the main part of the site -- with things like "Home, About Us, Services, Contact, Club Executive" along the top? Do I have to make it in html and put it in the "view" section? The tutorials I've read on rails (Getting Started With Rails) actually make the basic inventory system seem easy, compared to this part, using a lot of the built in functionality of Rails and scaffolding. The Rails documentation is a tad bit confusing.
The "official" Rails book is quite good if you want to start building Rails applications. link
But actually is something like this:
Create the rails app using rails applicationname
Create the controllers. It seesm that for you one controller is actually enough, name it for example main: ruby script/generate controller main
Now you have a controller in app/controllers, called main_controller.rb. Here you can insert the actions you want this controller to respond. If you don't want the controller to do anything just show the view, then leave the method empty.
class Main < ActionPack::Controllers
def index
end
def about
end
def contact
end
(...)
end
Now you got a controller that will respond to index, about and contact.
Create the views for this controller in app/views/main/index.erb (and others, like about.erb)
You can simply use HTML if you want
Alternatively you can use a layout, that you have to define in app/views/layouts/main.rhtml In this layout use HTML, but wherewer you want to include the view, write <%= yield %>
Example:
<HTML>
<BODY>
<%= yield %>
</BODY>
</HTML>
You can include this layout in the controller by writing layout :main in the class (before the method declarations)
Now if you run ruby script/server in the root of the app you can access the pages you've created. They will be static of course, but this might get you going. You have to add models and some logic to your controllers to advance. I advice you to check the book I linked if you're interested in more, or check the alternatives of rails like merb (http://merbivore.org) which has some nice features and is usually faster, but lacks the maturity of rails.
I picked up the book "Agile Web Development with Rails", and it's excellent. It goes through building an online grocery cart.
You should read about Model-View-Controller architecture if you have not yet done so, as it is the basis for most web frameworks including Ruby on Rails.
It sounds to me like this site might not be the best way to learn Ruby on Rails.
Rails is really great for CRUD applications (applications which allow users to Create, Read, Update, and Delete records in a database). Since your site looks to be all static pages except for the "Contact Us" section (which I'm assuming is a form that sends an email with some kind of confirmation page), you're actually going to find yourself kind of fighting against "The Rails Way."
Ideally in a situation like this, you could just throw all of your static pages into the public/ directory and make a quick Rails scaffold for the "Contact Us" page.
But by doing that, you won't end up with a finished project that resembles a typical Ruby on Rails application, and in the worst case, you might find yourself having to "unlearn" or at least "relearn" a lot of the aspects of Rails programming.
I think building a CRUD application with several resources (the canonical "Rails blog in 15 minutes" is a great start. You'll learn more by practicing Rails conventions and seeing the kind of workflow and application that really allows Rails to shine.
Then when it comes time to build another mostly-static website, you'll know exactly what you'll need to do to go about it.
My 2 cents, anyway.
Start off with Mephisto. It will give a framework to achieve your goal rather fast... otherwise you may simply flounder learning the gazillion things involved in creating the rails website.
With a simple site, I'd go for a Ruby micro-framework. The three I like are: Sinatra, Ramaze or _why's 4k Camping (get the one with the bugs fixed). RoR would be overkill.
I must recommend Ramaze. If you already know Ruby, but don't know Rails yet, Ramaze is better suited to you because it is "closer to home" as far as Ramaze apps being pure(r) Ruby.
For your DB access, you get a choice of ORMs. Sequel is most popular among Ramazers, but there's also DataMapper and M4DBI.
As Alan Alavi already said: You should familiarize yourself with MVC, but that can be done simply by diving in and getting your hands dirty.

Resources