Question

I replaced my custom inbox messaging system with the mailboxer gem. I have a issue with the Q&A feature on the app. The Question is not being sent to the users inbox so it can be answered. The question shows in the notifications table, however the conversation_id is NULL. With no conversation_id being generated the Question will never be delivered to the recipients inbox.

Can someone take a look at my code and see what went wrong?

Questions Controller:

  def index
    @questions = Question.all
    respond_with(@questions)
end

def show
  @question = Question.find(params[:id])
  @questions = Question.order("created_at DESC")
  respond_with(@questions)
end

def new
  @question = Question.new
  respond_with(@question)
end

def create
  @question = Question.new(params[:question])
  if @question.save
    #Original code @message = Message.create
    @message = current_user.messages.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

  def update
    @question = Question.find(params[:id])
    @question.update_attributes(:answer => params[:question][:answer])
    redirect_to user_messages_path(current_user, :mailbox => "inbox")
end
end

Messages Controller:

 def index
      redirect_to conversations_path(:box => @box)
    end

  # GET /message/new
  def new
    @message = current_user.messages.new
  end

   # POST /message/create
  def create
    @recipient = User.find(params[:user])
    current_user.send_message(@recipient, params[:body], params[:subject])
    flash[:notice] = "Message has been sent!"
    redirect_to :conversations
  end
  end

Message form:

 <%= @user %>
 <%= form_tag({controller: "messages", action: "create"}, method: :post) do %>
  <%= label_tag :subject %>
  <%= text_field_tag :subject %>
  <%= label :body, "Message text" %>
  <%= text_area_tag :body %>
  <%= text_field_tag(:user, "#{:user_id}") %>
  <%= submit_tag 'Send message', class: "btn btn-primary" %>
 <% end %>

Questions form:

<h1>New Question</h1>
<%= form_for @question do |f| %>
<%= @question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
    <ul>
        <li><%= f.text_field :question, {:placeholder => 'Please add your question...'} %></li>
    <%= f.hidden_field :sender_id, :value => current_user.id %>
        <li><%= f.hidden_field :recipient_id, :value => params[:user_id] %></li>
        <li><%= f.submit %></li>
        </ul>
        <% end %>
        <%= link_to "Back", questions_path, :class => "button" %>
Was it helpful?

Solution

Doesn't look like you set conversation_id anywhere? Try passing it as part of the options hash to current_user.messages.new(…)

OTHER TIPS

I think this is your problem:

<li><%= text_field_tag :question, {:placeholder => 'Please add your question...'} %></li>

this will set params[:question] to be a string. Your controller action is expecting it to be a hash. Maybe it should be

text_field_tag "question[text]" ...

or whatever the field that holds the text of the question is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top