1

我用邮箱 gem 替换了我的自定义收件箱消息系统。我对应用程序上的问答功能有疑问。问题未发送到用户收件箱,因此可以回答。问题显示在notifications表中,但conversation_id为 NULL。如果没有conversation_id生成问题,将永远不会发送到收件人收件箱。

有人可以看看我的代码,看看出了什么问题吗?

问题控制器:

  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

消息控制器:

 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

留言表格:

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

问题形式:

<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" %>
4

2 回答 2

1

看起来你没有在任何地方设置 conversation_id ?尝试将其作为选项哈希的一部分传递给 current_user.messages.new(...)

于 2014-04-22T13:33:47.823 回答
0

我认为这是你的问题:

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

这会将 params[:question] 设置为字符串。您的控制器操作期望它是一个散列。也许应该是

text_field_tag "question[text]" ...

或包含问题文本的任何字段。

于 2014-02-28T16:34:48.850 回答