I have a chat feature in a rails application im working on. Im using ajax to send messages. I decided to add paperclip onto my messages table, so you can send images in chat, but when i press enter the image appears for a second, its stored into the database, and then the page throws an error
MessagesController#create is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []
when i refresh the page the images shows up fine in the chat so i know the form submitted.
chatrooms.cofee
$(document).on "turbolinks:load", ->
$("#new_message").on "keypress", (e) ->
if e && e.keyCode == 13
e.preventDefault()
$(this).submit()
messages_controller.rb
class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_chatroom
def create
message = @chatroom.messages.new(message_params)
message.user = current_user
message.save
MessageRelayJob.perform_later(message)
end
private
def set_chatroom
@chatroom = Chatroom.find(params[:chatroom_id])
end
def message_params
params.require(:message).permit(:body, :image)
end
end
show.html.erb
<%= render 'side_nav' %>
<div class = "container-fluid">
<div class = "col-sm-10 col-sm-offset-2">
<p>
<strong>Name:</strong>
<%= @chatroom.name %>
</p>
<div data-behavior="messages" data-chatroom-id='<%= @chatroom.id %>'>
<% @messages.each do |message| %>
<%= render message %>
<% end %>
</div>
<%= form_for [@chatroom, Message.new], remote: true , multipart: true, authenticity_token: true do |f| %>
<%= f.text_field :body , rows: 1 , class: "form-control", autofocus:true%>
<%= f.file_field :image %>
<% end %>
</div>
sorry for the bad formatting, im new to stack overflow, but you can add as many chatrooms as you like, and im using ajax to send the messages. Im not sure if i need to include some sort of extra code to handle images. There is no error when sending basic strings, just the paperclip images.
Your create action will render by default. Since you don't have a template and don't want it to render you need to override it.
Instead of:
def create
message = @chatroom.messages.new(message_params)
message.user = current_user
message.save
MessageRelayJob.perform_later(message)
end
do something like :
def create
message = @chatroom.messages.new(message_params)
message.user = current_user
if message.save
MessageRelayJob.perform_later(message)
render json: {success: "message sent", error: false}, status: 200
else
render json: {success: false, error: true}, status: 422
end
end
eventually your browser clients will want some feedback as to whether or not the message was saved/sent.
The error you are getting is saying it is missing a create-template with format html. so... do you have a create.html.erb
? Because that is what Rails is telling you it is expecting...
If rendering that is not what you want... then you need to change how you call the create action. eg if you want it to be asking for the JS-version of the create.js.erb
template... then you need to make sure that format: js
goes into the link/form request.
Alternatively - you can redirect to the show page at the end of the create action :)
In your MessagesController you need to add the redirect in this way. "In the last line redirect_to chatroom_path(@chatroom) you redirect correctly"
def create
message = @chatroom.messages.new(message_params)
message.user = current_user
message.save
MessageRelayJob.perform_later(message)
redirect_to chatroom_path(@chatroom)
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.