2016-07-28 41 views
0

我想註釋多行的Rails 5控制器,我在網上搜索,發現了以下解決方案:「=開始」如何評論rails 5控制器中的多行?

=begin (Multiple lines) 
     respond_to do |format| 
     if @person.update_attributes(params[:person]) 
     flash[:notice] = 'Person was successfully updated.' 
     format.html { redirect_to(@person) } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @person.errors, :status => :unprocessable_entity } 
     end 
     end 
=end 

但它給這個錯誤:

syntax error, unexpected '=' =begin

我正在使用Rails 5.0。

回答

4

紅寶石多線評論僅供工作。確保線路開始與:

這工作:

=begin 
    foo 
    bar 
=end 

這是行不通的:

=begin 
    foo 
    bar 
    =end 
0

= begin和= end應該處於同一級別的縮進級別。像下面時,有該行的開始和之間沒有空格(同樣適用於=end

class MyController < ApplicationController 
=begin 
=end 
end 
+1

這是不完全正確的。註釋令牌的'='必須是行中的第一個符號,而不管這是否發生在類的代碼中。 –

+0

是的,我的意思是 –

+0

我同意Nic尼洛夫,雖然Sven Koschnicke更好地解釋它 –

0

=end之前他們

=begin 
    code 
    I 
    want 
    to 
    comment 
=end 
不得有任何空間

這與評論每行#

# code 
# I 
# want 
# to 
# comment 
0

除了其他答案,Ruby中的多行註釋語法雖然存在,但很少使用。有些編輯和語法熒光筆甚至不認識它。你最好的選擇是在每條註釋行上使用#

使用#的其他好處是嵌套註釋的選項,這是=begin ... =end語法不可行的選項。

respond_to do |format| 
    if @person.update_attributes(params[:person]) 
    # top level commented block 
    # flash[:notice] = 'Person was successfully updated.' 
    # # second level commented block 
    # # format.html { redirect_to(@person) } 
    # # format.xml { head :ok } 
    # ... 
    end 
end 

您的IDE應該沒有問題每次評論/取消註釋單個評論級別。

查看this SO question的討論。

+0

嗯,但是如果我們處理一個巨大的代碼然後評論每一行不會是一個好主意,很多時間會浪費 –

+0

你應該使用您的IDE工具而不是手動輸入每個符號。多行註釋快捷鍵基本上都可用。 –

+0

Niolv嗯...... okkk謝謝:) –