所以我創建了一個基於https://www.sitepoint.com/build-a-messaging-system-with-rails-and-actioncable/Rails的自定義郵件系統,談話是不是正確創建
這偉大的工作,直到我遇到了它不能正確地創建新的對話的一個錯誤的自定義消息系統。 (談話在我的代碼名爲insconversation)
class PersonalMessagesController
...
def create
@insconversation ||= Insconversation.create(author_id: current_user.id, //This part seems to be having issues
receiver_id: @receiver.id)
@personal_message = current_user.personal_messages.build(personal_message_params)
@personal_message.insconversation_id = @insconversation.id
@personal_message.save!
flash[:success] = "Your message was sent!"
redirect_to insconversation_path(@insconversation)
end
...
消息本身保存到Mysql數據庫,但它的父「insconversation_id」留下空,同樣在不添加新的對話的「insconversation」表。我使用了render:text =>和insconversation,它的id都是null,(因爲未能創建)。
我recived的errror消息是在redirect_to的部分 沒有路由匹配{:行動=> 「節目」,:控制器=> 「insconversations」:ID =>零}缺少必需的鍵:[:ID]
而且個人信息觀看
view/Personal_Message
<h1>New message to <%= @receiver.name %></h1> //receiver is properly identified
<%= form_for @personal_message do |f| %>
<%= hidden_field_tag 'receiver_id', @receiver.id %>
<%= f.label :body %>
<%= f.text_area :body, size: "60x3", required:true %>
<%= f.submit %>
<% end %>
會話控制器
class InsconversationsController < ApplicationController
before_action :set_insconversation, except: [:index]
before_action :check_participating!, except: [:index]
def index
@insconversations = Insconversation.participating(current_user).order('updated_at DESC')
end
def show
@insconversation = Insconversation.find_by(id: params[:id])
@personal_message = PersonalMessage.new
end
private
def set_insconversation
@insconversation = Insconversation.find_by(id: params[:id])
end
def check_participating!
redirect_to root_path unless @insconversation && @insconversation.participates?(current_user)
end
end
編輯:
我剛纔檢查的SQL日誌
Insconversation Exists (0.2ms) SELECT 1 AS one FROM `insconversations` WHERE `insconversations`.`author_id` = 2 AND `insconversations`.`receiver_id` = 3 LIMIT 1
(0.2ms) ROLLBACK
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO `personal_messages` (`body`, `user_id`, `created_at`, `updated_at`) VALUES ('8', 2, '2017-04-24 00:31:11', '2017-04-24 00:31:11')
系統似乎覺得Inconversation已經存在。
既沒有效果,第二個重定向到會話列表,但它不能解決永遠不會在SQL表中創建新對話行的問題。我使用phpmyadmin來檢查表格 – Jay
@Jay好吧。那麼讓我們從它拋出的錯誤開始,這是一個路由和參數錯誤。另外,我提出的第二個解決方案是將您路由到索引,但我有點好奇。運行'rails routes'並列出與'insconversation'有關的路由。 – codelitt
insconversations_path \t GET \t /insconversations(.:format)\t insconversations#指數 insconversation_path \t GET \t /insconversations/:id(.:format)\t insconversations#顯示 – Jay