Search code examples
ruby-on-railsrubyruby-on-rails-4mailboxer

formal argument cannot be an instance variable def trash_folder @trash ||= current_user.mailbox.trash.all end ^


I'm implementing the mailboxer gem into my app and thought this should work but getting the error above, I think it has to do with ||= operator

I'm getting this

    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 view index:

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

and link to the inbox in the current_user_session path

<%= link_to "inbox", conversations_path %>

I have other views but I think the issue is in the conversations controller. I'm not sure what's going on with these errors, it should work


Solution

  • Put your method definitions on multiple lines like so:

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

    When you put it all on one line, your @trash variable is being interpreted as a method parameter. I would really advise against any one line methods, they is difficult to read and can be confusing due to ruby's optional paren rules.