【发布时间】:2020-10-30 11:57:03
【问题描述】:
在将 Bootstrap 添加到项目之前一切正常。现在我收到ActionController::UnknownFormat 错误。我之前在我的项目中使用过 jquery(添加了纱线)。但是在安装 Bootstrap 时,我还用 gem 为它安装了 jquery。也许那里可能有问题。感谢您的帮助。
cmets_controller.rb
def create
@book = Book.find(params[:book_id])
@comment = @book.comments.create(comment_params)
respond_to :js
end
create.js.erb
$(".comment").append("<%= escape_javascript(render(@comment))%>");
_comment.html.erb
<p><strong><%= comment.title %></strong> | <%= comment.commenter %></p>
<p><%= comment.content %></p>
控制台输出
app/controllers/comments_controller.rb:17:in `create'
Started POST "/books/2/comments" for 127.0.0.1 at 2020-10-30 14:45:28 +0300
Processing by CommentsController#create as HTML
Parameters: {"authenticity_token"=>"PT6Go3HJURAbU7olooyAVa7j5Xna9NlfulH62DPt/JbcfRXw1cWEujnfNhhjmafyncB9tMkefUKT09z+d0ryiQ==", "comment"=>{"title"=>"a", "content"=>"s", "commenter"=>"user", "approved"=>"false"}, "commit"=>"Create Comment", "book_id"=>"2"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Book Load (0.2ms) SELECT "books".* FROM "books" WHERE "books"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/comments_controller.rb:15:in `create'
(0.1ms) BEGIN
↳ app/controllers/comments_controller.rb:16:in `create'
Comment Create (0.4ms) INSERT INTO "comments" ("title", "content", "approved", "book_id", "created_at", "updated_at", "commenter") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["title", "a"], ["content", "s"], ["approved", false], ["book_id", 2], ["created_at", "2020-10-30 11:45:28.088322"], ["updated_at", "2020-10-30 11:45:28.088322"], ["commenter", "utkucb"]]
↳ app/controllers/comments_controller.rb:16:in `create'
(0.8ms) COMMIT
↳ app/controllers/comments_controller.rb:16:in `create'
Completed 406 Not Acceptable in 9ms (ActiveRecord: 1.8ms | Allocations: 5589)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/comments_controller.rb:17:in `create'
我正在使用此表单在 books/show.html.erb 中访问CommentsController#create。
<%= form_with(model: [ @book, @book.comments.build ], local: false) do |form| %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :content %><br>
<%= form.text_area :content %>
</p>
<%= form.hidden_field :commenter, value: current_user.username %>
<%= form.hidden_field :approved, value: false %>
<p>
<%= form.submit %>
</p>
<% end %>
我的 Rails 版本是 Rails 6.0.3.4,而 Bootstrap 版本是 5.0.0.alpha1。我使用this教程来使用jquery,一切都很好。比使用this 添加Bootstrap。
【问题讨论】:
-
Processing by CommentsController#create as HTML-- 做了什么改变使它成为一个 HTML 请求,而不是一个 JS 请求? -
您能否提供代码,您是如何访问此
CommentsController#create的?我认为格式选项只是丢失,因此生成错误的 url -
@zhisme 我已将其添加到问题中。
-
@TomLord 我找不到它,但是当我尝试注销时,我也收到
No route matches [GET] "/users/sign_out"错误。这可能是关于 jquery 的。我见过一些将require jquery添加到application.js 的解决方案。但这并没有解决问题。 -
在您的表单助手中尝试
format: :js。
标签: ruby-on-rails ruby