如何在没有重定向的情况下在Rails 5中实现对另一个控制器动作的远程表单?

时间:2019-03-23 15:13:32

标签: ruby-on-rails ajax forms ruby-on-rails-5

尝试提交在另一个控制器中具有创建操作但不重定向到页面或呈现页面的表单(仅停留在页面上)。

错误:

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();

0 个答案:

没有答案