Save 36% for Black Friday! Learn more

All threads / Rails 6 form_with will not pass resources with Association
Ask A Question


You’re not receiving notifications from this thread.

Rails 6 form_with will not pass resources with Association

Jim Miller asked in Rails

I am using Rails 6. I have a form that I need to pass remote: true so I get POST to process as JS:

LOG: MessagesController#create as JS

Here is what I have tried:

<%= form_for [@hangout,] do |f| %>

The result is a good save to the DB but processes as HTML:

LOG: MessagesController#create as HTML

So I tried:

<%= form_for [@hangout,], remote: true do |f| %>

I learned that this would give an InvalidAuthenticityToken error:

LOG: ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:

Tried this:

<%= form_for [@hangout,], authenticity_token: true do |f| %>

It passes as HTML, not JS

I read that for Rails 6, the best way to do this was with form_with because it passes remote:true:

<%= form_with(model: [@hangout,]) do |f| %>

Unfortunately, this never reaches the controller so I get no response. I know that my model and controller are set up properly since the first try with form_for works, so it has to be with the way I am writing my form_with, right?

Does anyone have any advice?


class Message < ApplicationRecord
  belongs_to :hangout
  belongs_to :user

class MessagesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_hangout
  def create
    message =
    message.user = current_user
    redirect_to @hangout
    def set_hangout
      @hangout = Hangout.find(params[:hangout_id])
    def message_params

require 'sidekiq/web'

Rails.application.routes.draw do
  resources :hangouts do
    resource :hangout_users
    resources :messages

  resources :notes

  authenticate :user, lambda { |u| u.admin? } do
    mount Sidekiq::Web => '/sidekiq'

  devise_for :users, controllers: { registrations: 'users/registrations' }

  get 'mine', to: 'notes#mine'

  root to: 'application#root'

  mount Shrine.presign_endpoint(:cache) => '/images/upload'

BTW, I am working on this Lesson:
Group Chat with ActionCable: Part 3

Updated my controller but still no go:

def create
  message =
  message.user = current_user
  respond_to do |format|
      format.html { redirect_to @hangout, notice: 'Success' }
      format.html { render action: 'new' }
Join the discussion

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

Join 67,819+ 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.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.