All threads / How to post remote action and no redirect? ActionCable Series

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to post remote action and no redirect? ActionCable Series

BrandonKlotz asked in Rails

Trying to submit a form that has a create action in another controller but does not redirect to or render the page (just stay on the page).

Error:

MessagesController#create is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []

Form:

  = form_for [@room, Message.new], remote: true do |f| 
    .field
      = f.text_field :body, placeholder: "Message #{@room.name}", autofocus: true

Remote Controller:

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
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,629+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.