0

メールボックスの 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/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 %>

他の意見もありますが、問題は会話コントローラーにあると思います。これらのエラーで何が起こっているのかわかりませんが、うまくいくはずです

4

2 に答える 2

2

defセミコロンを使用せずにメソッドの内容を同じ行に置くことはできません。

メソッドを 1 行にまとめたい場合は、次のようにリファクタリングします。

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

編集

私の答えは完全に正しいわけではありません。Jörg がコメントで述べたように、セミコロンなしで 1 行でメソッドを定義することは完全に可能です。Ruby は、パラメーター リストがどこで終了し、メソッドの本体が開始するかを知る必要があるだけです。これは、改行、セミコロン、または空のパラメーター リストを使用して実現できます。

于 2014-04-14T21:15:29.413 に答える
0

次のように、メソッド定義を複数の行に配置します。

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

すべてを 1 行にまとめると、@trash変数はメソッド パラメーターとして解釈されます。1行のメソッドは読みにくく、Rubyのオプションの括弧ルールのために混乱を招く可能性があるため、絶対にしないことをお勧めします。

于 2014-04-14T21:16:01.940 に答える