尝试提交在另一个控制器中具有创建操作但不重定向到页面或呈现页面的表单(仅停留在页面上)。
错误:
MessagesController#create is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []
表格(rooms / show.html.haml):
= form_for [@room, Message.new], remote: true do |f|
.field
= f.text_field :body, placeholder: "Message #{@room.name}", autofocus: true
遥控器操作:
class MessagesController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :authenticate_user!
before_action :set_room
def create
message = @room.messages.new(message_params)
message.user = current_user
message.save
MessageRelayJob.perform_later(message)
end
private
def set_room
@room = current_user.rooms.find(params[:room_id])
end
def message_params
params.require(:message).permit(:body)
end
end
messages / create.js.erb
$("#new_message")[0].reset();