2014-02-28 17 views
1

我用郵箱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" %> 
+0

如果你看看你的日誌文件,你應該看到傳遞給控制器​​的參數是什麼。然後,您可以瀏覽控制器代碼並查看問題發生的位置。一旦你看到params就可能很明顯。如果params中有什麼問題,那麼提交給控制器的表單有問題。 –

+0

順便說一句,當你傳遞一個字符串而不是一個散列到update_attributes時,你會得到stringify_keys方法。所以這聽起來像params [:question]是一個字符串,而不是散列。 –

+0

在您的問題控制器中創建操作嘗試將'@question = Question.new(params [:question])''改爲'@question = Question.create(params [:question])''。新建僅創建本地對象,但不會嘗試驗證或將其保存到數據庫。您已在New操作中創建了本地對象。 – winston

回答

1

看起來你不像在任何地方設置conversation_id?嘗試將它作爲選項散列的一部分傳遞給current_user.messages.new(...)

0

我覺得這是你的問題:

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

這將設置PARAMS [:提問]是一個字符串。你的控制器操作期望它是一個散列。也許它應該是

text_field_tag "question[text]" ... 

或任何包含問題文本的字段是。

+0

產生與去除佔位符相同的錯誤。我將視圖切換爲使用form_for,而我可以將該問題提交給Conversations表內的數據庫,而不會出現錯誤。唯一的問題是不會產生conversation_id(保留NULL),這意味着問題永遠不會發送到收件人收件箱。我將新視圖添加到原始帖子的底部。 –