rails6
https://qiita.com/take18k_tech/items/00cc14c0eff45073ffc7
https://qiita.com/rhiroe/items/4c4e983e34a44c5ace27
I am trying to implement the group chat function while referring to the above.
When I created the create.js.erb file, I get the following error when executing create and cannot proceed.
I was able to confirm that the data was normally passed to self in message.rb.
routes.rb
resources: recruits do
resources: rooms, only: [: index,: create]
end
index.html.erb
<% = form_with (model: @message, url: recruit_rooms_path, method:: post, class:'needs-validation', html: {novalidate: false}) do | f |%>
<% = f.text_field: content, class: "form-control", id: "message_content", placeholder: "Enter message (up to 200 characters)"%>
<% = f.submit "submit", class: "btn btn-primary input-group-append", id: "message-button"%>
<% end%>
room_channel.rb
def subscribed
stream_from "room_channel_ # {params ['recruit']}"
end
/ * global $messageContainer * /
import consumer from "./consumer";
document.addEventListener ('turbolinks: load', () =>{// Define variables for use in js.erb
window.messageContainer = document.getElementById ('message-container');
if (messageContainer === null) {// Prevent the following programs from running on other pages
return;
}
consumer.subscriptions.create ({channel:'RoomChannel', recruit: $('# message-container'). data ('recruit_id')}, {
connected () {
console.log ("connected");
},
disconnected () {
},
// What was broadcast on room_channel.rb arrives here
received: function (data) {
console.log ("received");
// Add the HTML received from the server side at the very end
messageContainer.insertAdjacentHTML ('beforeend', data ['message']);
}
});
});
rooms_controller.rb
def index
@recruit = Recruit.find (params [: recruit_id])
@messages = Message.where (recruit_id: @recruit) .includes (: user,: recruit) .order (created_at :: desc) .page (params [: page]) .per (30)
@message = Message.new
Abbreviation
end
def create
@message = current_user.messages.new (message_params)
@ message.recruit = Recruit.find (params [: recruit_id])
@ message.save
ActionCable.server.broadcast'room_channel', message: @ message.template
end
message.rb
belongs_to: recruit
belongs_to: user
validates: content, presence: true, length: {maximum: 200}
# Convert posted message to HTML with partial template for message
def template
ApplicationController.renderer.render partial:'messages/message', locals: {message: self}
end
create.js.erb
// Erase the string entered in the form
console.log ('create.js.erb');
document.getElementById ('message_content'). value ='';
When I create the create.html.erb file, I get rid of the error but go to the empty create page.
I don't know how to load js.erb or something else. I would appreciate it if you could give me some advice
-
Answer # 1
-
Answer # 2
https://qiita.com/rhiroe/items/4c4e983e34a44c5ace27
I implemented the chat function in another way, referring to.The code, problems that occurred, and solutions can be found at the links below.
https://stackoverflow.com/questions/309684
Related articles
- [ruby on rails 6] i implemented the tag function using acts-as-taggable-on, but i can't jump to the linked article from the list
- ruby on rails - implementation of validation function
- ruby on rails - cart function routing error cannot be resolved
- ruby on rails - addition of like function to users to flea market apps
- ruby on rails - i want to implement a function for administrator users to register luggage for general users in rails
- ruby on rails - with the product listing function, an error message appears only for the product description
- ruby on rails - i can't implement the rails like function
- ruby on rails - i want to implement a report function, but i get an actioncontroller :: urlgenerationerror
- ruby - post photo&like function on rails self-introduction page
- ruby - rails pagination function i want to display in alphabetical order
- ruby on rails 6 - [rails]'unfollow' function does not work * no error message
- ruby on rails - i implemented it so that images can be saved in s3 of aws, but the view can no longer be displayed
- ruby on rails - [rails] i want to implement a filtering function and display the corresponding items on the list screen
- ruby on rails - i want to implement a function (destroy action) that can delete posted messages, but i am getting an active reco
- ruby on rails - comment posting function actioncontroller :: urlgenerationerror
- ruby - [rails] the cart function cannot get the cart id associated with the user
- ruby on rails - how to implement a sort function by the value of the statistical column that aggregates the values of related
- ruby - rails comment function display
- ruby - how nice! function routes do not pass well (relation) [rails]
- ruby on rails - [rails6] actioncable data transfer does not work
- ruby - [rails6] i get an error on heroku when i write
- javascript - i want to eliminate error 404
- ruby - i want to transition to the specified show screen
- ruby - dealing with validation errors when rendering rails apps (definition of instance variables)
- ruby - about rails controller new action
- ruby on rails - no response when trying to launch ec2 rails
- ruby - sns login i don't know how to install twitter api
- ruby on rails - i want to implement a login function using devise
If you want the request to be JS, use form_with or the helper path option
format: js
AddOr