2014-04-14 17 views
0

我正在實施郵箱gem到我的應用程序,並認爲這應該工作,但得到上述錯誤,我認爲它與|| =操作做正式參數不能是一個實例變量def trash_folder @trash || = current_user.mailbox.trash.all結束^

我得到這個

conversations_controller.rb:16: formal argument cannot be an instance variable def  

trash_folder @trash ||= current_user.mailbox.trash.all end^ 

/home/action/booklist/app/controllers/conversations_controller.rb:16: syntax error,  

unexpected tOP_ASGN, expecting ';' or '\n' def trash_folder @trash ||= 

current_user.mailbox.trash.all end^

/home/action/booklist/app/controllers/conversations_controller.rb:18: syntax error, unexpected '.', expecting ';' or '\n' def trash conversation.move_to_trash(current_user) 

...^/home/action/booklist/app/controllers/conversations_controller.rb:18: syntax 

error, unexpected tIDENTIFIER, expecting end-of-input ...rash(current_user) redirect_to 

:conversations end ...^

Conversations_controller:

class ConversationsController < ApplicationController 

helper_method :mailbox, :conversation 


def index 
@conversations ||= current_user.mailbox.inbox.all 
end 


def reply 
    current_user.reply_to_conversation(conversation, *message_params(:body, :subject)) 
redirect_to conversation 
end 

def trash_folder  @trash ||= current_user.mailbox.trash.all end 

def trash conversation.move_to_trash(current_user) redirect_to :conversations end 

def untrash conversation.untrash(current_user) redirect_to :back end 

def empty_trash current_user.mailbox.trash.each do |conversation|  conversation.receipts_for(current_user).update_all(:deleted => true) 
end 
redirect_to :conversations 
end 


private 

def mailbox 
@mailbox ||= current_user.mailbox 
end 

def conversation 
@conversation ||= mailbox.conversations.find(params[:id]) 
end 

def conversation_params(*keys) 
fetch_params(:conversation, *keys) 
end 

def message_params(*keys) 
fetch_params(:message, *keys) 
end 

def fetch_params(key, *subkeys) 
params[key].instance_eval do 
    case subkeys.size 
when 0 then self 
when 1 then self[subkeys.first] 
else subkeys.map{|k| self[k] } 
    end 

end 

end 

會話視圖索引:

<% @conversations.each do |conversation| %> 

<% if participant != current_user %> 
<%= participant.name, participant %> 
<% end %> 

<%= link_to conversation.subject, conversation %> 
<%= conversation.updated_at.strftime("%a, %m/%e/%Y %I:%M %p") %> 
<%= link_to "Move to Trash", {:controller => "conversations", :action => "trash", :id => conversation.id}, :title=> "Move to Trash", :method=>'post' %> 
<% end %> 

,並鏈接到收件箱中current_user_session路徑

<%= link_to "inbox", conversations_path %> 

我還有其他的意見,但我認爲這個問題是在對話控制器。我不確定這些錯誤發生了什麼,它應該可以工作

+1

這裏的格式化是一場完全災難。有什麼方法可以修復它並使其更具可讀性? – tadman

回答

0

把多條線路上的方法定義如下所示:

def trash_folder 
    @trash ||= current_user.mailbox.trash.all 
end 

當你把它全部在同一行,你@trash變量被解釋爲方法的參數。我真的會反對任何一種方法,由於ruby的可選paren規則,它們很難閱讀並且可能會令人困惑。

2

您不能在不使用分號的情況下將方法內容放在與def相同的行上。

如果你想你的方法是在同一行,重構他們看起來像這樣:

def trash_folder; @trash ||= current_user.mailbox.trash.all; end 

編輯

我的答案是不完全正確的。正如Jörg在評論中所述,完全可以在一行中定義一個沒有分號的方法。 Ruby只需要知道參數列表已經完成並且方法的主體開始。這可以通過使用換行符,分號或空參數列表來實現。

+0

當然你可以:'def trash_folder()@trash || = current_user.mailbox.trash.all end' –

+0

相關提示Jörg。我沒有意識到這一點。 – Zajn

+1

重要的是Ruby需要知道參數列表已經完成並且正文開始。這可以通過表達式分隔符(換行符或分號)或空參數列表來實現。 –

相關問題