我用邮箱 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" %>