Ruby on rails 形式参数不能是实例变量def trash_folder@trash | |=current_user.mailbox.trash.all end^
我正在我的应用程序中实现mailboxer gem,并认为这应该可以工作,但得到上面的错误,我认为这与| |=运算符有关 我明白了Ruby on rails 形式参数不能是实例变量def trash_folder@trash | |=current_user.mailbox.trash.all end^,ruby-on-rails,ruby,ruby-on-rails-4,mailboxer,Ruby On Rails,Ruby,Ruby On Rails 4,Mailboxer,我正在我的应用程序中实现mailboxer gem,并认为这应该可以工作,但得到上面的错误,我认为这与| |=运算符有关 我明白了 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
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 ... ^
主控台:
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
类会话控制器true)
结束
重定向到:对话
结束
私有的
def邮箱
@邮箱| |=当前用户邮箱
结束
def对话
@对话| |=邮箱.对话.查找(参数[:id])
结束
def对话参数(*键)
获取参数(:对话,*键)
结束
def消息参数(*键)
获取参数(:消息,*键)
结束
def fetch_参数(键,*子键)
参数[key]。实例\u评估do
大小写子键
当0时,则为self
当1时,则为self[子键.first]
else子键.map{| k | self[k]}
结束
结束
结束
对话视图索引:
<% @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 %>
“对话”、:操作=>“垃圾箱”、:id=>conversation.id}、:title=>“移动到垃圾箱”、:方法=>'post%%>
并链接到当前用户会话路径中的收件箱
<%= link_to "inbox", conversations_path %>
我有其他观点,但我认为问题在于对话控制器。我不确定这些错误到底是怎么回事,它应该可以工作如果不使用分号,就不能将方法的内容与其
def
放在同一行上
如果希望方法在一行上,请将其重构为如下所示:
def trash_folder; @trash ||= current_user.mailbox.trash.all; end
编辑
我的回答不完全正确。正如Jörg在评论中所说,完全可以在一行定义一个方法,而不使用分号。Ruby只需要知道参数列表在哪里完成,方法体从哪里开始。这可以通过使用换行符、分号或空参数列表来实现。将方法定义放在多行上,如下所示:
def trash_folder
@trash ||= current_user.mailbox.trash.all
end
当您将所有内容放在一行时,
@trash
变量将被解释为方法参数。我真的建议不要使用任何一行方法,因为ruby的可选paren规则,这些方法很难阅读,而且可能会让人困惑。这里的格式是一场灾难。有没有办法修复它并使其更具可读性?当然可以:def trash|u folder()@trash|124;=current_user.mailbox.trash.all end
很高兴认识Jörg。我没有意识到这一点,重要的是Ruby需要知道参数列表已经完成,主体已经开始。这可以通过表达式分隔符(换行符或分号)或空参数列表实现。