This is my first question!
We are trying to apply asynchronous communication to the posting function of the posted application currently in production at ruby on rails.
The create action uses response_to to take data in json format and ajax returns the data.
However, when I tried to implement it, I couldn't get out of one error and I couldn't keep posting.
Please refer to the following information:
Unable to autoload constant MessagesController, expected/Users/namae/projects/chat-space/app/controllers/messages_controller.rb to define it
The error and
Uncaught SyntaxError: Unexpected token.
compile error
Multiple errors appear.
Below is the code around asynchronous communication.
[messages_controller]
def create
@[email protected](message_params)
[email protected]
response_to do | format |
format.html {redirect_to group_messages_path(@group)}
format.json
else
@[email protected](:user)
flash.now [:alert] = "Please enter a message"
render —index
end
end
[message.js]
$(function(){
function buildHTML(data){
var Image=';
if(passage.image){
Image=`<img src=${message.image} class=`lower-message__image`>`
}
varhtml=`<div class="message" data-message-id=${message.id}>
<div class="upper-message">
<div class="upper-message__user-name">
${ message.name}
</div>
<div class="upper-message__date">
${ message.data}
</div>
</div>
<div class="lower-mesage">
<p class="lower-message__content">
${ message.text}
</p>
$ {Image}
</div>
</div>`;
return html;
}
$("#new_message").on('submit', function(e){// When you submit the form
console.log("this")
e.preventDefault();// Stop the action and stop the page transition
var formData = new FormData(this); // Get form information
var url = $(this).attr("action");
$.ajax({
type: "POST",
url —url,
data —FormData,
dataType: "json",
processData: false,
contentType:false
});
.done(function(data){
var html = buildHTML(data);
var url = $(this).attr("action");
$('.message') .append(html);
})
.fail(function(){
alert('error');
});
return false;
});
});
[create.json.jbuilder]
json.id@m essage.id
[email protected]
[email protected]
[email protected]_at.strftime('%Y/%m/%d%H:%M:%S')
json.name@m essage.user.name
There are many things that are not good enough, but if you understand, please take care of me.
javascript ruby-on-rails ruby ajax asynchronous
I thought there was no mistake wherever I looked for the controller.
In the end, the message_controller create action missed the end of response_to.
Thank you for your cooperation!
First, let's look at the error message.
"Unable to autoload constant MessagesController, expected/Users/namae/projects/chat-space/app/controllers/messages_controller.rb to define it"
(Direct translation: The constant MessagesController cannot be autoloaded.
I expected it to be defined in /Users/namae/projects/chat-space/app/controllers/messages_controller.rb...)
Therefore, please check the contents of messages_controller.rb.
Where is the source of the messages_controller.rb in the question?
Was there any explanation on the site?
==
© 2024 OneMinuteCode. All rights reserved.