2013-02-19 59 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代碼有所不同。