Cornelius Wilson
Cornelius Wilson

Reputation: 2914

wrong number of arguments (0 for 3..6)

I am using the mailboxer gem and I am looking to have Questions sent to the Users inbox so they can view and answer the question.

I am receiving a Argument Error wrong number of arguments (0 for 3..6). It points to the line @message = current_user.send_message.new(:subject => "You have a question from #{@question.sender_id}",

I was trying to use the send message instance since the original line of code @message = current_user.messages.new(:subject => "You have a question from #{@question.sender_id}" would submit the Question to the notifications table, but not connect to the conversations table.

Questions Controller:

def create
  @question = Question.new(params[:question])
  if @question.save
    #Original code @message = Message.create
    @message = current_user.send_message.new(:subject => "You have a question from #{@question.sender_id}",
                           #Original code :sender_id
                           :notification_id => @question.sender_id,
                           #Original code :recipient_id
                           :receiver_id => @question.recipient_id,
                           :body => @question.question)

    @question.message = @message
    @question.save
    redirect_to questions_path, notice: 'Your question was saved successfully. Thanks!'
  else
    render :new, alert: 'Sorry. There was a problem saving your question.'
  end
end

Upvotes: 1

Views: 289

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53018

You are getting the error because send_message method (provided by mailboxer gem) requires 3..6 arguments and you are not sending any arguments to it.

Also, looking at your code, it seems like you are trying to save a message record for a question. So, your @message should be set as:

@message = current_user.message.new(:subject => "You have a question from #{@question.sender_id}",
                           #Original code :sender_id
                           :notification_id => @question.sender_id,
                           #Original code :recipient_id
                           :receiver_id => @question.recipient_id,
                           :body => @question.question)
@question.message = @message
@question.save

Note that I used current_user.message.new instead of current_user.send_message.new.

EDIT

For using send_message, you at least need to pass 3 arguments:

send_message(recipient, "Body", "subject")

where recipient is the person receiving message.

I am not sure why you are chaining new method after send_message(as it is usually called to create a new instance of a Class).

You can call send_message as follows:

 @message = current_user.send_message(@question.recipient_id, @question.question, "You have a question from #{@question.sender_id}") 

Upvotes: 1

Related Questions