2017-04-23 45 views
0

所以我創建了一個基於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已經存在。

回答

1

模型上的ID由Rails自動設置,因此您評論自己遇到問題的代碼部分不是實際問題。問題是與該行:

redirect_to insconversation_path(@insconversation) 

該消息指出,您缺少參數insconversation ID params。你只需要稍微調整一下這行就可以得到ID。

redirect_to insconversation_path(@insconversation.id) 

或者你可以做

redirect_to @insconversation 

和Rails會推斷休息。

+0

既沒有效果,第二個重定向到會話列表,但它不能解決永遠不會在SQL表中創建新對話行的問題。我使用phpmyadmin來檢查表格 – Jay

+0

@Jay好吧。那麼讓我們從它拋出的錯誤開始,這是一個路由和參數錯誤。另外,我提出的第二個解決方案是將您路由到索引,但我有點好奇。運行'rails routes'並列出與'insconversation'有關的路由。 – codelitt

+0

insconversations_path \t GET \t /insconversations(.:format)\t insconversations#指數 insconversation_path \t GET \t /insconversations/:id(.:format)\t insconversations#顯示 – Jay

0

你還需要這個修正嗎?我遇到了同樣的問題。使用這個:

class PersonalMessagesController < ApplicationController 
    before_action :find_conversation! 

    def new 
    redirect_to conversation_path(@conversation) and return if @conversation 
    @personal_message = current_user.personal_messages.build 
    end 

    def create 
    @conversation ||= Conversation.create(author_id: current_user.id, 
             receiver_id: @receiver.id) 
    @personal_message = current_user.personal_messages.build(personal_message_params) 
    @personal_message.conversation_id = @conversation.id 
    @personal_message.save! 

    flash[:success] = "Your message was sent!" 
    redirect_to conversation_path(@conversation) 
    end 

    private 

    def personal_message_params 
    params.require(:personal_message).permit(:body) 
    end 

    def find_conversation! 
    if params[:receiver_id] 
     @receiver = User.find_by(id: params[:receiver_id]) 
     redirect_to(root_path) and return unless @receiver 
     @conversation = Conversation.between(current_user.id, @receiver.id)[0] 
    else 
     @conversation = Conversation.find_by(id: params[:conversation_id]) 
     redirect_to(root_path) and return unless @conversation && @conversation.participates?(current_user) 
    end 
    end 
end 
相關問題