我的Rails 4应用中有一个远程form_for
,它没有请求JavaScript响应。当我单击表单上的提交时,我收到一个UnknownFormat错误。据说参数是:
{"utf8"=>"✓",
"authenticity_token"=>"eAzS1lELwDL9Qt+fM2hAPlPmCxCgFPuAvl1QYWOIL3IBPdrnxn0woYYC+5uzGjyqH3lQxGxWGrYq/fnMrFNfnw==",
"artist"=>{"name"=>"Joe"},
"commit"=>"Save Artist"}
我的表单代码是:
= form_for(@artist, remote: true, authenticity_token: true) do |f|
- if @artist.errors.any?
#error_explanation
%h2= "#{pluralize(@artist.errors.count, "error")} prohibited this artist from being saved:"
%ul
- @artist.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name
.actions
= f.submit 'Save Artist'
我有一个create.js.erb
文件,我的create
中的ArtistsController
方法如下:
def new
@artist = Artist.new
respond_to do |format|
format.js
end
end
def create
@artist = Artist.new(artist_params)
if @artist.save
respond_to do |format|
format.js
end
end
end
我的节目表格是:
= form_for @show, html: {role: 'form'} do |f|
- if @show.errors.any?
#error_explanation
%h2= "#{pluralize(@show.errors.count, "error")} prohibited this venue from being saved:"
%ul
- @show.errors.full_messages.each do |msg|
%li= msg
.row
= f.label :venue_id
= f.collection_select :venue_id, Venue.all, :id, :name, class: 'form-control'
.row
.col-sm-6
= link_to 'New Artist', new_artist_path, id: :new_artist_link, remote: true
.row.row-centered
.col-sm-6
.form-group
= f.label :date
= f.date_select :show_date, order: [:day, :month, :year], class: 'form-control'
.col-sm-6
.form-group
= f.label :doors_open
= f.time_select :doors_open, class: 'form-control'
.row.row-centered
.col-sm-6
.form-group
= f.label :dinner_starts
= f.time_select :dinner_starts, class: 'form-control'
.col-sm-6
.form-group
= f.label :show_starts
= f.time_select :show_starts, class: 'form-control'
.row.row-centered
.col-sm-6
.form-group
= f.label :dinner_ends
= f.time_select :dinner_ends, class: 'form-control'
.col-sm-6
.form-group
= f.label :show_ends
= f.time_select :show_ends, class: 'form-control'
.form-group
= f.submit 'Save Show', class: 'btn btn-default'
我的艺术家视图中的new.js.erb
文件是:
$('#new_artist_link').hide().after("<%= j render 'form' %>");
我的艺术家观点中的create.js.erb
是:
$('#new_artist').remove();
控制台输出:
tarted GET "/artists/new" for 127.0.0.1 at 2015-02-26 22:46:12 -0600
Processing by ArtistsController#new as JS
Rendered artists/_form.html.haml (2.7ms)
Rendered artists/new.js.erb (4.2ms)
Completed 200 OK in 15ms (Views: 8.8ms | ActiveRecord: 0.2ms)
Started POST "/artists" for 127.0.0.1 at 2015-02-26 22:46:16 -0600
Processing by ArtistsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0dhmOLTBguCAn4gdaIE0D0PyokHp4RLLZ8DDeqiA21Wo6W4JI7dyc/vfrBno80ibD235lSWj8/3zYGrXZ1uruA==", "artist"=>{"name"=>"fsa"}, "commit"=>"Save Artist"}
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "artists" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "fsa"], ["created_at", "2015-02-27 04:46:16.795869"], ["updated_at", "2015-02-27 04:46:16.795869"]]
(209.8ms) commit transaction
Completed 406 Not Acceptable in 213ms
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/artists_controller.rb:20:in `create'
Rendered /home/ryan/.rvm/gems/ruby-2.2.0@DoseyDoe/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_source.erb (5.1ms)
Rendered /home/ryan/.rvm/gems/ruby-2.2.0@DoseyDoe/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_trace.html.erb (3.0ms)
Rendered /home/ryan/.rvm/gems/ruby-2.2.0@DoseyDoe/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/ryan/.rvm/gems/ruby-2.2.0@DoseyDoe/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_web_console.html.erb (0.8ms)
Rendered /home/ryan/.rvm/gems/ruby-2.2.0@DoseyDoe/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/diagnostics.html.erb within rescues/layout (26.7ms)
编辑:我不知道它是否有所作为,但远程表格是另一种模式的形式。
编辑2:修复了创建方法 - 仍然没有骰子。添加了我正在使用的表单模板链接到创建艺术家。
编辑3:添加了控制台输出
编辑4:添加了create.js.erb
答案 0 :(得分:3)
尝试将format
参数添加到form_for
。您默认请求html
响应,而您的控制器不知道如何回答。请求js
响应应该修复它。
= form_for(@artist, format: js, authenticity_token: true) do |f|
答案 1 :(得分:0)
只是遇到了同样的错误,经过数小时的调试发现,rails ujs
是必需的,但未正确加载。
如果您使用的是webpack
,请确保使用start
ujs
。就我而言,我忘记了start();
选项,即
import Rails from '@rails/ujs';
Rails.start();
或
require("@rails/ujs").start();
如果您喜欢require
语法。