I am trying to get some .js on my rails app and I have 1 problem.
There is already one same question, but answer is not good for me also.
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(params[:comment].permit(:name, :body))
@comment.name = current_user.email
respond_to do |format|
if @comment.save
format.js
redirect_to article_path(@article)
else
end
end
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
end
There is also Create.js.erb file
$('#forma2 table').append("<%= j render @comment %>")
And error from terminal
Redirected to http://localhost:3000/articles/90
Completed 406 Not Acceptable in 27ms (ActiveRecord: 10.5ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/comments_controller.rb:7:in `create'
TL;DR Remove the redirect after format.js
You can't respond with JS and then redirect. Redirects are for HTTP requests. Removing the redirect should fix your issue. If you want to handle HTTP requests as well then add format.html
before the redirect.
If you want to redirect you can pass in the redirect URL into your JS and from there manipulate URL with window.location or something.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.