Why Many People Use "-%>" Instead of "%>" in Rails

Why many people use -% instead of % in Rails?

I would like to add some resources that I know about ERB :

  • Rails extends ERB, so that you can suppress the newline simply by adding a trailing hyphen to tags in Rails templates:

    <ul>
    <% for @item in @items -%>
    <li><%= @item %></li>
    <% end -%>
    </ul>
  • Comment markers use a hash sign:

     <%# This is just a comment %>
  • A tag with an equals sign indicates that enclosed code is an expression, and that the renderer should substitute the code element with the result of the code (as a string) when it renders the template. Use an expression to embed a line of code into the template, or to display the contents of a variable:

     Hello, <%= @name %>.
    Today is <%= Time.now.strftime('%A') %>.
  • With one equal sign the string will be encoded. To avoid encoding, you can use two equals signs (or raw):

        Hello, <%== @unencodedOutput %>
  • Tags without the equals sign denote that the enclosed code is a scriptlet. Each scriptlet is caught and executed, and the final result of the code is then injected in to the output at the point of the scriptlet.

    <ul>
    <% for @item in @shopping_list %>
    <li><%= @item %></li>
    <% end %>
    </ul>

    Scriptlets are most commonly used for embedding loops or conditional logic into templates:

Read An Introduction to ERB Templating to know more.

Why everyone use double quotes instead of simple quote?

Double quotes allows you to do interpolation : "Number of users : #{@count_user}"

Plus taking a look at the benchmarks, I'd say that at best it doesn't matter, the overhead is very small, and some benchmarks are actually faster with double quotes ...

In Rails, shouldn't we create a service layer, instead of jumbling logic into a controller?

The "Rails way" is: skinny controllers, fat models.

You can simply change the model to support cache:

class User < ActiveRecord::Base
def self.all
@cached[:all] ||= super
end
end

Or create an injector to support cache the way you want for multiple models:

class User < ActiveRecord::Base
include CacheInjector
end

Remember: Ruby, as a dynamic language, is very easy to extend. Mixins, interceptors, aspects, all those things that are a PITA to implement in Java, are very easy and natural on Ruby. Give it a try.

Why would I want to use unicorn or thin instead of WEBrick for development purposes?

It is important to develop as closely as possible to the production environment. It helps ensure that an application will work as expected when deployed into production, instead of stumbling upon bugs at runtime.

This issue is alleviated with the use of Continuous Testing on a Build server that replicates the production environment. Even though you are not actively developing on an identical environment, the Continuous Testing gives you coverage that the application is functioning in the expected way.

As to speed, the performance hit running a Rails app in development mode will negate any benefit the various web servers brings.

How to deal with resource usage of Rails? Is it good to hack everything?

Use the latest and greatest versions to get performance benefits.

Try using Rubinius instead of MRI Ruby. Try using Ruby v1.9.2 instead of Ruby v1.8.7. Try to use Rails v3.0.x or v3.1.

@meagar is correct that you shouldn't "hack" anything. You should do the exact opposite, which is called doing it the "Rails Way".

One mantra of Rails is "Convention over Configuration". If you find yourself hacking, you are probably doing it wrong.

Rails Displaying Full Database Instead of of Attributes Only

Remove = from

<%= @g.tickets.each do |tick| %>

It should look like this

<% @g.tickets.each do |tick| %>

what is ruby used for BESIDES rails?

Firstly, PHP was built to be a server language, first and foremost. That's one of the primary reasons it's not usually used for anything other than web applications - it's not designed for it and therefore can't match up to the requirements of, say, a scripted desktop application.

Ruby was designed as a general-purpose scripting language, and thus has a wide support for a number of different applications. It has been used across the board for everything from web applications, to web servers themselves, to intelligent graphing libraries, to picture recognition engines, to threaded database servers, to low-level system utilities. It has a wide spectrum of use throughout computing.

Rails has most certainly boosted the popularity of Ruby by a huge amount, brought it up into the forefront of scripting languages, spawning 40+ books, 18 conferences, distribution with nearly all the major operating systems and 7 different implementations to boot!

To begin with, all of Rails' internals are written in Ruby itself, but all of the Rubygems, libraries, snippets or anything else that is used by Rails developers is written in Ruby too, and is most likely built to work outside of the Rails stack.

Having said that, the popular web framework is not the reason why Ruby is popular. Sure, it has helped bring much-needed attention to the language, but it's not the reason why people use it day to day. It's used for everything from web applications to desktop GUI applications because of it's simple, elegant syntax, it's clean, sensible and complete standard library, it's wide ecosystem of community code, support and toolchains.

Ruby is used because it rocks.

Is there a technical reason not to use _url instead of _path in a Rails URL?

As a rule of thumb

  • Use _path for internal links
  • Use _url for external and CDN type links

Pros of using _path

  • Hassle free domain migration
  • Faster page loading since you are reducing the page size


Related Topics



Leave a reply



Submit