0

I am trying to teach myself Ruby and Ruby on Rails. Can someone give me some quick pointers on this code from http://guides.rubyonrails.org/getting_started.html

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="errorExplanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited
      this post from being saved:</h2>
      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Questions:

  1. Sometimes the code uses <% and sometimes uses <%=. When do I use either of these?
  2. What exactly does @ mean in Ruby and when is is used?
  3. Besides for the HTML, is all of Ruby code, or there some new code/syntax which Ruby on Rails introduces?
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user1015214
  • 2,733
  • 10
  • 36
  • 66
  • http://stackoverflow.com/questions/11247480/what-is-the-difference-between-and-in-ruby-on-rails – Michael Berkowski Feb 19 '13 at 21:49
  • And http://stackoverflow.com/questions/14319347/variables-in-ruby-on-rails – Michael Berkowski Feb 19 '13 at 21:50
  • all those questions are answered numerous times. do you mind using google or stackoverflow's search capabilities? – Iuri G. Feb 19 '13 at 21:50
  • These are all very general questions about basic rails functionality, so you're not likely to get a useful answer here. You just need to work your way through a few tutorials and read a few books on Ruby. Rails does introduce a lot of methods and classes, but aside from html, css, and javascript, the syntax is all Ruby. – Zach Kemp Feb 19 '13 at 21:51

1 Answers1

2
  1. <%= outputs the result of the expression to the page. <% just evaluates it.
  2. @ indicates an instance variable in a class. In Rails, it's also how you expose a controller variable to it's view.
  3. Rails does not add any new syntax to Ruby, although there are some conventions which may look different from 'traditional' Ruby code.
Andy Waite
  • 10,785
  • 4
  • 33
  • 47