when you enter a message in the form and click the "send" button, the message is not created, and, accordingly, is not displayed in the window. that a message is not created, checked by logs and consoles - in the logs there is a query in the SELECT database "messages". * FROM "messages" ORDER_BY created_at ASC, when prompted in the console Message.find (: id), for example 1, or 2, or 3 - gives an error, because the message was not created.

Message model belongs to User model

controllers/messages_controller.rb :

 class MessagesController < ApplicationController def new @message = Message.new @messages = Message.all.order('created_at ASC') end def create if current_user @message = current_user.messages.build(message_params) if @message.save flash[:success] = 'created successfully' else flash[:error] = 'oops, an error!' end end redirect_to root_path end private def message_params params.require(:message).permit(:body) end end 

index.html.erb :

 <div class="panel panel-default"> <div class="panel-body"> <ul class="media-list"> <%= render @messages %> </ul> </div> </div> 

_message.html.erb

 <li class="media-message"> <%= link_to image_tag(message.user.avatar_url, alt: message.user.name, class: "media-object"), message.user.profile_url, target: '_blank', class: 'pull-left' %> <div class="media-body"> <h4 class="media-heading"> <%= link_to message.user.name, message.user.profile_url, target: '_blank' %> says <small class="text-muted">[at <%= message.created_at.strftime('%-d %B %Y, %H:%M:%S') %>]</small> </h4> <p><%= message.body %></p> </div> </li> 

http://khersonchat.herokuapp.com/ site

$ heroku logs

 User Load (5.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] Processing by MessagesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"XYtmzo3Fq2psbRUWNR8w8iZFSVIIbjxAKX+Q7psmB5vWxXtFO8r2hLASGEPypomWt/L0wKHmI3d7+D1kw0Kiiw==", "message"=>{"body"=>"Hello world!"}, "commit"=>"Отправить"} Started POST "/messages" for 134.249.158.132 at 2016-03-31 17:30:48 +0000 (1.8ms) BEGIN (2.2ms) ROLLBACK Redirected to http://khersonchat.herokuapp.com/ Completed 302 Found in 38ms (ActiveRecord: 9.9ms) Started GET "/" for 134.249.158.132 at 2016-03-31 17:30:48 +0000 Processing by MessagesController#new as HTML at=info method=POST path="/messages" host=khersonchat.herokuapp.com request_id=dcef82d9-4ff0-4319-88f6-f8c547db7867 fwd="134.249.158.132" dyno=web.1 connect=1ms service=53ms status=302 bytes=1168 Rendered collection (0.0ms) Rendered messages/_form.html.erb (1.2ms) Message Load (3.3ms) SELECT "messages".* FROM "messages" ORDER BY created_at ASC User Load (3.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] Rendered messages/new.html.erb within layouts/application (17.0ms) Completed 200 OK in 20ms (Views: 11.7ms | ActiveRecord: 7.0ms) 
  • during initialization a new message is not created <- uh, what? - D-side
  • @ D-side, I apologize, I just sit over this error and my brain has already flowed: (I will try to explain how an adequate person is - when I enter the message text into the form, and click on the "send" button, the message is not created, and, accordingly, is displayed in the window that the message is not being created, I checked on the logs and the console - the query in the SELECT "messages". * FROM "messages" ORDER_BY created_at ASC database is in the logs SELECT "messages". * FROM "messages" ORDER_BY created_at ASC , but when I try to find the message in the console - Message.find(:id) , for example, 1, or 2, or 3 - gives an error, because the message was not created - AlexNikolaev94
  • So when creating a combo there begin / insert / commit is in the logs? - D-side
  • @ D-side, alas, no. and I just can not understand what the error is :( I rechecked the controller and models several times - AlexNikolaev94
  • Ok, then bring the logs themselves, honor. - D-side

1 answer 1

The problem was validations. instead of length: { maximum: 2000 } I specified length: { minimum: 2000 } . You should always carefully read the code! :)