I have a comments controller that i want to load a partial "create.js.erb"
class CommentsController < ApplicationController
skip_before_action :verify_authenticity_token
def create
@comment = Comment.new(comment_params)
@comment.account_id = current_account.id
respond_to do |format|
if @comment.save
@comments = Comment.where(post_id: @comment.post_id)
format.js { render "comments/create" }
else
# unable to save
end
end
end
def comment_params
params.require(:comment).permit(:message, :post_id)
end
end
My create.js.erb partial
console.log("comment created...");
$("#post-comments").html("<%= escape_javascript(render partial: 'posts/comments', locals: { comments: @comments }) %>");
If i refresh the page the comment gets posted however when i click the submit button it send me the error ActionController::UnknownFormat in CommentsController#create
In laymans terms i want the create function to hit without having to redirect.
Seems like using Hotwire and Turbo is the way to go
then you need to change create.js.erb to create.turbo_stream.erb
all works as intended now
Rails 7 removes the previous way of using format.js in Rails UJS and now it's all hotwire/turbo. Kinda confusing but once you learn it no more writing javascript. :)