0

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

franklinexpress
  • 1,149
  • 14
  • 44
  • 1
    The formatting here is a total disaster. Is there any way to fix it and make it more readable? – tadman Apr 14 '14 at 21:17

2 Answers2

2

You cannot put a method's contents on the same line as its def without using semi-colons.

If you want your methods to be on one line, refactor them to look like this:

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

EDIT

My answer isn't entirely correct. As Jörg stated in the comments, it is entirely possible to define a method on one line without semi-colons. Ruby just needs to know where the parameter list has finished and the body of the method begins. This can be achieved by using a newline, a semi-colon, or an empty parameter list.

Zajn
  • 4,078
  • 24
  • 39
  • Of course you can: `def trash_folder() @trash ||= current_user.mailbox.trash.all end` – Jörg W Mittag Apr 15 '14 at 01:07
  • Good to know Jörg. I wasn't aware of that. – Zajn Apr 15 '14 at 11:22
  • 1
    The important thing is that Ruby needs to know that the parameter list is finished and the body begins. This can be achieved with an expression separator (newline or semicolon) or with an empty parameter list. – Jörg W Mittag Apr 15 '14 at 11:25
0

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.

Alex.Bullard
  • 5,533
  • 2
  • 25
  • 32