Rails: Action Cable saves only first record in database











up vote
0
down vote

favorite












I'm currently struggling to implement ActionCable into my application. The problem that I'm facing is that only the first message is saved into the databse and only the first message is updated in the view.



Every message afterwards isn't saved into the db. The console gives me not much information, it just says messages_controller.rb line 17 rollback transaction and few lines below Completed 204 No Content in xxxxx.ms



Command Prompt



Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:33 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:34 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: Broken pipe
MessagesChannel is transmitting the subscription confirmation
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:34 +0100
MessagesChannel is streaming from conversation_8
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_9
Started POST "/conversations/8/messages" for 127.0.0.1 at 2018-11-10 21:11:56 +0100
Processing by MessagesController#create as JS
Parameters: {"utf8"=>"✓", "message"=>{"context"=>"jhhjhkh", "user_id"=>"9"}, "commit"=>"Absenden", "conversation_id"=>"8"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Conversation Load (0.0ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = ? LIMIT ? [["id", 8], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:31
(0.0ms) begin transaction
↳ app/controllers/messages_controller.rb:17
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."context" = ? LIMIT ? [["context", "jhhjhkh"], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."conversation_id" = ? LIMIT ? [["conversation_id", 8], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."user_id" = ? LIMIT ? [["user_id", 9], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
(0.0ms) rollback transaction
↳ app/controllers/messages_controller.rb:17
No template found for MessagesController#create, rendering head :no_content
Started GET "/cable" for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Completed 204 No Content in 13166ms (ActiveRecord: 0.0ms)


Started GET "/cable" for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: Broken pipe
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from conversation_8
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_9
Started GET "/" for 127.0.0.1 at 2018-11-10 21:12:29 +0100
Processing by PagesController#home as HTML
Rendering pages/home.html.erb within layouts/application
Rendered shared/_footer.html.erb (4.0ms)
Rendered pages/home.html.erb within layouts/application (132.0ms)
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ app/views/shared/_navbar.html.erb:21
Rendered shared/_navbar.html.erb (176.0ms)
Rendered shared/_message.html.erb (0.0ms)
Completed 200 OK in 2052ms (Views: 1943.4ms | ActiveRecord: 8.0ms)


My messages controller



class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation

def index
if current_user == @conversation.sender || current_user == @conversation.recipient
@other = current_user == @conversation.sender ? @conversation.recipient : @conversation.sender
@messages = @conversation.messages.order("created_at DESC")
else
redirect_to conversations_path, alert: "You don't have permission."
end
end

def create
@message = @conversation.messages.new(message_params)
@messages = @conversation.messages.order("created_at DESC")
if @message.save
ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
end
end


private

def render_message(message)
self.render(partial: 'messages/message', locals: {message: message})
end

def set_conversation
@conversation = Conversation.find(params[:conversation_id])
end

def message_params
params.require(:message).permit(:context, :user_id)
end
end









share|improve this question






















  • it seems like a model validation failure. change save to save!, that will raise an exception and will show the reason for transaction rollback.
    – PGill
    Nov 11 at 7:38















up vote
0
down vote

favorite












I'm currently struggling to implement ActionCable into my application. The problem that I'm facing is that only the first message is saved into the databse and only the first message is updated in the view.



Every message afterwards isn't saved into the db. The console gives me not much information, it just says messages_controller.rb line 17 rollback transaction and few lines below Completed 204 No Content in xxxxx.ms



Command Prompt



Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:33 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:34 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: Broken pipe
MessagesChannel is transmitting the subscription confirmation
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:34 +0100
MessagesChannel is streaming from conversation_8
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_9
Started POST "/conversations/8/messages" for 127.0.0.1 at 2018-11-10 21:11:56 +0100
Processing by MessagesController#create as JS
Parameters: {"utf8"=>"✓", "message"=>{"context"=>"jhhjhkh", "user_id"=>"9"}, "commit"=>"Absenden", "conversation_id"=>"8"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Conversation Load (0.0ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = ? LIMIT ? [["id", 8], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:31
(0.0ms) begin transaction
↳ app/controllers/messages_controller.rb:17
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."context" = ? LIMIT ? [["context", "jhhjhkh"], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."conversation_id" = ? LIMIT ? [["conversation_id", 8], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."user_id" = ? LIMIT ? [["user_id", 9], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
(0.0ms) rollback transaction
↳ app/controllers/messages_controller.rb:17
No template found for MessagesController#create, rendering head :no_content
Started GET "/cable" for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Completed 204 No Content in 13166ms (ActiveRecord: 0.0ms)


Started GET "/cable" for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: Broken pipe
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from conversation_8
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_9
Started GET "/" for 127.0.0.1 at 2018-11-10 21:12:29 +0100
Processing by PagesController#home as HTML
Rendering pages/home.html.erb within layouts/application
Rendered shared/_footer.html.erb (4.0ms)
Rendered pages/home.html.erb within layouts/application (132.0ms)
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ app/views/shared/_navbar.html.erb:21
Rendered shared/_navbar.html.erb (176.0ms)
Rendered shared/_message.html.erb (0.0ms)
Completed 200 OK in 2052ms (Views: 1943.4ms | ActiveRecord: 8.0ms)


My messages controller



class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation

def index
if current_user == @conversation.sender || current_user == @conversation.recipient
@other = current_user == @conversation.sender ? @conversation.recipient : @conversation.sender
@messages = @conversation.messages.order("created_at DESC")
else
redirect_to conversations_path, alert: "You don't have permission."
end
end

def create
@message = @conversation.messages.new(message_params)
@messages = @conversation.messages.order("created_at DESC")
if @message.save
ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
end
end


private

def render_message(message)
self.render(partial: 'messages/message', locals: {message: message})
end

def set_conversation
@conversation = Conversation.find(params[:conversation_id])
end

def message_params
params.require(:message).permit(:context, :user_id)
end
end









share|improve this question






















  • it seems like a model validation failure. change save to save!, that will raise an exception and will show the reason for transaction rollback.
    – PGill
    Nov 11 at 7:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm currently struggling to implement ActionCable into my application. The problem that I'm facing is that only the first message is saved into the databse and only the first message is updated in the view.



Every message afterwards isn't saved into the db. The console gives me not much information, it just says messages_controller.rb line 17 rollback transaction and few lines below Completed 204 No Content in xxxxx.ms



Command Prompt



Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:33 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:34 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: Broken pipe
MessagesChannel is transmitting the subscription confirmation
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:34 +0100
MessagesChannel is streaming from conversation_8
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_9
Started POST "/conversations/8/messages" for 127.0.0.1 at 2018-11-10 21:11:56 +0100
Processing by MessagesController#create as JS
Parameters: {"utf8"=>"✓", "message"=>{"context"=>"jhhjhkh", "user_id"=>"9"}, "commit"=>"Absenden", "conversation_id"=>"8"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Conversation Load (0.0ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = ? LIMIT ? [["id", 8], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:31
(0.0ms) begin transaction
↳ app/controllers/messages_controller.rb:17
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."context" = ? LIMIT ? [["context", "jhhjhkh"], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."conversation_id" = ? LIMIT ? [["conversation_id", 8], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."user_id" = ? LIMIT ? [["user_id", 9], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
(0.0ms) rollback transaction
↳ app/controllers/messages_controller.rb:17
No template found for MessagesController#create, rendering head :no_content
Started GET "/cable" for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Completed 204 No Content in 13166ms (ActiveRecord: 0.0ms)


Started GET "/cable" for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: Broken pipe
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from conversation_8
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_9
Started GET "/" for 127.0.0.1 at 2018-11-10 21:12:29 +0100
Processing by PagesController#home as HTML
Rendering pages/home.html.erb within layouts/application
Rendered shared/_footer.html.erb (4.0ms)
Rendered pages/home.html.erb within layouts/application (132.0ms)
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ app/views/shared/_navbar.html.erb:21
Rendered shared/_navbar.html.erb (176.0ms)
Rendered shared/_message.html.erb (0.0ms)
Completed 200 OK in 2052ms (Views: 1943.4ms | ActiveRecord: 8.0ms)


My messages controller



class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation

def index
if current_user == @conversation.sender || current_user == @conversation.recipient
@other = current_user == @conversation.sender ? @conversation.recipient : @conversation.sender
@messages = @conversation.messages.order("created_at DESC")
else
redirect_to conversations_path, alert: "You don't have permission."
end
end

def create
@message = @conversation.messages.new(message_params)
@messages = @conversation.messages.order("created_at DESC")
if @message.save
ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
end
end


private

def render_message(message)
self.render(partial: 'messages/message', locals: {message: message})
end

def set_conversation
@conversation = Conversation.find(params[:conversation_id])
end

def message_params
params.require(:message).permit(:context, :user_id)
end
end









share|improve this question













I'm currently struggling to implement ActionCable into my application. The problem that I'm facing is that only the first message is saved into the databse and only the first message is updated in the view.



Every message afterwards isn't saved into the db. The console gives me not much information, it just says messages_controller.rb line 17 rollback transaction and few lines below Completed 204 No Content in xxxxx.ms



Command Prompt



Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:33 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:34 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: Broken pipe
MessagesChannel is transmitting the subscription confirmation
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:11:34 +0100
MessagesChannel is streaming from conversation_8
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_9
Started POST "/conversations/8/messages" for 127.0.0.1 at 2018-11-10 21:11:56 +0100
Processing by MessagesController#create as JS
Parameters: {"utf8"=>"✓", "message"=>{"context"=>"jhhjhkh", "user_id"=>"9"}, "commit"=>"Absenden", "conversation_id"=>"8"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Conversation Load (0.0ms) SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = ? LIMIT ? [["id", 8], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:31
(0.0ms) begin transaction
↳ app/controllers/messages_controller.rb:17
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."context" = ? LIMIT ? [["context", "jhhjhkh"], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."conversation_id" = ? LIMIT ? [["conversation_id", 8], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
Message Exists (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."user_id" = ? LIMIT ? [["user_id", 9], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:17
(0.0ms) rollback transaction
↳ app/controllers/messages_controller.rb:17
No template found for MessagesController#create, rendering head :no_content
Started GET "/cable" for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Completed 204 No Content in 13166ms (ActiveRecord: 0.0ms)


Started GET "/cable" for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: Broken pipe
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-11-10 21:12:11 +0100
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from conversation_8
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notification_9
Started GET "/" for 127.0.0.1 at 2018-11-10 21:12:29 +0100
Processing by PagesController#home as HTML
Rendering pages/home.html.erb within layouts/application
Rendered shared/_footer.html.erb (4.0ms)
Rendered pages/home.html.erb within layouts/application (132.0ms)
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 9], ["LIMIT", 1]]
↳ app/views/shared/_navbar.html.erb:21
Rendered shared/_navbar.html.erb (176.0ms)
Rendered shared/_message.html.erb (0.0ms)
Completed 200 OK in 2052ms (Views: 1943.4ms | ActiveRecord: 8.0ms)


My messages controller



class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation

def index
if current_user == @conversation.sender || current_user == @conversation.recipient
@other = current_user == @conversation.sender ? @conversation.recipient : @conversation.sender
@messages = @conversation.messages.order("created_at DESC")
else
redirect_to conversations_path, alert: "You don't have permission."
end
end

def create
@message = @conversation.messages.new(message_params)
@messages = @conversation.messages.order("created_at DESC")
if @message.save
ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
end
end


private

def render_message(message)
self.render(partial: 'messages/message', locals: {message: message})
end

def set_conversation
@conversation = Conversation.find(params[:conversation_id])
end

def message_params
params.require(:message).permit(:context, :user_id)
end
end






ruby-on-rails ruby-on-rails-5 actioncable






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:32









klasarn

126




126












  • it seems like a model validation failure. change save to save!, that will raise an exception and will show the reason for transaction rollback.
    – PGill
    Nov 11 at 7:38


















  • it seems like a model validation failure. change save to save!, that will raise an exception and will show the reason for transaction rollback.
    – PGill
    Nov 11 at 7:38
















it seems like a model validation failure. change save to save!, that will raise an exception and will show the reason for transaction rollback.
– PGill
Nov 11 at 7:38




it seems like a model validation failure. change save to save!, that will raise an exception and will show the reason for transaction rollback.
– PGill
Nov 11 at 7:38












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










This is because of validation failure for @message. So, Update your create action to something like this.



def create
@message = @conversation.messages.new(message_params)
@messages = @conversation.messages.order("created_at DESC")
if @message.save
ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
else
Rails.logger.info "Errors: #{@message.errors.full_messages}"
end
end


Observe the else part, It will print validation errors to console. In an ideal scenario, you would want to show these error messages to users.




Also, you can use if @message.save! this will create an exception.
@message.save just returns true or false







share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243152%2frails-action-cable-saves-only-first-record-in-database%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    This is because of validation failure for @message. So, Update your create action to something like this.



    def create
    @message = @conversation.messages.new(message_params)
    @messages = @conversation.messages.order("created_at DESC")
    if @message.save
    ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
    else
    Rails.logger.info "Errors: #{@message.errors.full_messages}"
    end
    end


    Observe the else part, It will print validation errors to console. In an ideal scenario, you would want to show these error messages to users.




    Also, you can use if @message.save! this will create an exception.
    @message.save just returns true or false







    share|improve this answer

























      up vote
      0
      down vote



      accepted










      This is because of validation failure for @message. So, Update your create action to something like this.



      def create
      @message = @conversation.messages.new(message_params)
      @messages = @conversation.messages.order("created_at DESC")
      if @message.save
      ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
      else
      Rails.logger.info "Errors: #{@message.errors.full_messages}"
      end
      end


      Observe the else part, It will print validation errors to console. In an ideal scenario, you would want to show these error messages to users.




      Also, you can use if @message.save! this will create an exception.
      @message.save just returns true or false







      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        This is because of validation failure for @message. So, Update your create action to something like this.



        def create
        @message = @conversation.messages.new(message_params)
        @messages = @conversation.messages.order("created_at DESC")
        if @message.save
        ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
        else
        Rails.logger.info "Errors: #{@message.errors.full_messages}"
        end
        end


        Observe the else part, It will print validation errors to console. In an ideal scenario, you would want to show these error messages to users.




        Also, you can use if @message.save! this will create an exception.
        @message.save just returns true or false







        share|improve this answer












        This is because of validation failure for @message. So, Update your create action to something like this.



        def create
        @message = @conversation.messages.new(message_params)
        @messages = @conversation.messages.order("created_at DESC")
        if @message.save
        ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
        else
        Rails.logger.info "Errors: #{@message.errors.full_messages}"
        end
        end


        Observe the else part, It will print validation errors to console. In an ideal scenario, you would want to show these error messages to users.




        Also, you can use if @message.save! this will create an exception.
        @message.save just returns true or false








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 16:51









        Abhilash Reddy

        978616




        978616






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243152%2frails-action-cable-saves-only-first-record-in-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma