我用郵箱gem替換了我的自定義收件箱郵件系統。我在應用程序的Q &功能上遇到問題。該問題未被髮送到用戶收件箱,因此可以回答。該問題顯示在notifications
表中,但conversation_id
爲NULL。如果沒有生成conversation_id
,問題將永遠不會傳遞給收件人收件箱。修復conversation_id從NULL
有人可以看看我的代碼,看看出了什麼問題嗎?
問題控制器:
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操作中創建了本地對象。 – winston