首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >形式参数不能是实例变量def trash_folder @杂质||= current_user.mailbox.trash.all current_user.mailbox.trash.all^^

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

Stack Overflow用户
提问于 2014-04-14 21:10:37
回答 2查看 1.7K关注 0票数 0

我在我的应用程序中实现了邮箱创业板,并认为这应该可以工作,但是得到上面的错误,我认为这与||=操作符有关。

我来接这个

代码语言:javascript
运行
复制
    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:

代码语言:javascript
运行
复制
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

会话视图索引:

代码语言:javascript
运行
复制
<% @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路径中的收件箱。

代码语言:javascript
运行
复制
<%= link_to "inbox", conversations_path %>

我有其他的观点,但我认为问题在于谈话负责人。我不知道这些错误是怎么回事,它应该管用

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-14 21:16:01

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

代码语言:javascript
运行
复制
def trash_folder
  @trash ||= current_user.mailbox.trash.all
end

当您将所有内容放在一行时,您的@trash变量将被解释为一个方法参数。我真的建议不要使用任何一行方法,因为它们很难读懂,而且由于ruby的可选的paren规则,可能会让人感到困惑。

票数 0
EN

Stack Overflow用户

发布于 2014-04-14 21:15:29

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

如果希望您的方法位于一行上,请将它们重构为如下所示:

代码语言:javascript
运行
复制
def trash_folder; @trash ||= current_user.mailbox.trash.all; end

编辑

我的回答不完全正确。正如J rg在评论中所指出的,在一行上定义一个没有分号的方法是完全可能的。Ruby只需要知道参数列表在哪里完成,方法的主体就开始了。这可以通过使用换行符、分号或空参数列表来实现.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23070485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档