2013-02-19 57 views
0

我想教自己Ruby和Ruby on Rails。有人可以给我这个代码的一些简单的指针从http://guides.rubyonrails.org/getting_started.html获取Ruby语法直接

<%= 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 %> 

问题:

  1. 有时代码使用<%,有时使用<%=。我什么时候使用其中任何一种?
  2. @在Ruby中的含义是什么,何时被使用?
  3. 除了HTML,还有Ruby代码,还是Ruby on Rails引入的一些新的代码/语法?
+0

http://stackoverflow.com/questions/11247480/what-is-the-difference-between-and-in-ruby-on-rails – 2013-02-19 21:49:15

+0

和http://stackoverflow.com/questions/14319347/变数在红宝石轨道 – 2013-02-19 21:50:09

+0

所有这些问题都回答了无数次。你介意使用谷歌或stackoverflow的搜索功能? – 2013-02-19 21:50:52

回答

2
  1. <%=输出表达式到页面的结果。 <%只是评估它。
  2. @指示类中的实例变量。在Rails中,它也是如何将控制器变量展示给它的视图。
  3. Rails没有向Ruby添加任何新语法,尽管有一些约定可能与'传统'Ruby代码有所不同。