形式参数不能是实例变量def trash_folder @trash || = current_user.mailbox.trash.all end ^

时间:2014-04-14 21:10:37

标签: ruby-on-rails ruby ruby-on-rails-4 mailboxer

我正在将boyer gem实现到我的应用程序中并认为这应该可行但是得到上面的错误,我认为它与|| = operator

有关

我收到了这个

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

我有其他观点,但我认为问题出在对话控制器中。我不确定这些错误是怎么回事,它应该可行

2 个答案:

答案 0 :(得分:2)

如果不使用分号,您不能将方法的内容放在与其def相同的行上。

如果您希望您的方法在一行上,请将它们重构为:

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

修改

我的回答并不完全正确。正如Jörg在评论中所说,完全可以在没有分号的情况下在一条线上定义方法。 Ruby只需知道参数列表的完成位置以及方法体开始。这可以通过使用换行符,分号或空参数列表来实现。

答案 1 :(得分:0)

将您的方法定义放在多行上,如下所示:

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

当您将所有内容放在一行时,您的@trash变量将被解释为方法参数。我真的建议反对任何一行方法,它们很难阅读,并且由于ruby的可选paren规则而容易引起混淆。