【发布时间】:2014-02-28 14:14:51
【问题描述】:
我用邮箱 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" %>
【问题讨论】:
-
如果你查看你的日志文件,你应该看到在 params 中传递给控制器的确切内容。然后,您可以单步执行控制器代码并查看问题发生在哪里。一旦你看到参数,它可能很明显。如果 params 中的内容有问题,则提交给控制器的表单有问题。
-
顺便说一句,当您将字符串而不是哈希传递给 update_attributes 时,您会得到 stringify_keys 方法。所以听起来 params[:question] 是一个字符串,而不是一个哈希。
-
在您的问题控制器中创建操作尝试将
@question = Question.new(params[:question])更改为@question = Question.create(params[:question])。 New 仅创建本地对象,但不会尝试验证或将其保存到数据库中。您已经在 New 操作中创建了本地对象。 -
我认为你应该稍微改变一下工作流程。尝试更改我上面提到的内容,以便您可以创建问题并将其保存到数据库中。保存问题后,我认为您的问题控制器应该能够在创建消息时获取该保存的对象并将其附加到您的正文参数。
-
@james 问题正在使用原始创建操作
@question = Question.new(params[:question])保存到数据库中。问题是没有创建会话 ID。如果没有 conversation_id,问题将永远不会发送到用户收件箱。我认为问题出在表单本身,而不是控制器。