新手问题 在 create 动作中 调用 js 失效 ,表单写了 remote:true 但反映问题 ActionController::UnknownFormat

SunA0 · 2020年07月13日 · 最后由 Rei 回复于 2020年07月14日 · 1048 次阅读

ruby 2.4.1 rails 5.2.3

worker_fields_controller

def new
  @field = WorkerField.new 
end

def create
    @field = WorkerField.new(field_params)
    @field.save
    respond_to do |format|
      format.js
    end
end

new.html.erb

<%= form_for(@field, remote: true,authenticity_token: true,html: {class: "navbar-left"}) do |form| %>
  <%= form.text_field :field_name,id:'tag_name',class: 'form-control' ,placeholder: "标签名"%>
  <%= form.submit "创建", class: "btn btn-primary " %>
<% end %>

create.js.erb

console.log("111111111111111111")

报错:ActionController::UnknownFormat

Request info:

Request parameters  
{"utf8"=>"", "authenticity_token"=>"6voe3N6JOISSuzCArQjVZnnhFLnrd7y2IOgemnZbxNdoFuhsouWqmP7PH0JA/iS0uVjnoFE+coaTEh/UNqoLMA==", "hr_worker_field"=>{"field_name"=>"xxx"}, "controller"=>"hr/worker_fields", "action"=>"create"}
Rack session    
(Object too large. Modify ActionDispatch::Request::Session#inspect or adjust BetterErrors.maximum_variable_inspect_size if you need to see it.)

log:

Started POST "/hr/worker_fields" for ::1 at 2020-07-13 19:24:02 +0800
Processing by Hr::WorkerFieldsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"fTZiZhfkuxYwMC7+vUeOM0dcEuPslmTLHbJD5k/mJJn/2pTWa4gpClxEATxQsX/hh+Xh+lbfqvuuSEKoDxfrfg==", "hr_worker_field"=>{"field_name"=>"xxx"}}
  Fdn::User Load (0.4ms)  SELECT  `fdn_users`.* FROM `fdn_users` WHERE `fdn_users`.`id` = 1 LIMIT 1
  ↳ app/helpers/fdn/user_sessions_helper.rb:22
  Fdn::UserInformation Load (0.3ms)  SELECT `fdn_user_informations`.* FROM `fdn_user_informations` WHERE `fdn_user_informations`.`user_id` = 1
  ↳ app/helpers/fdn/user_sessions_helper.rb:22
   (0.1ms)  BEGIN
  ↳ app/controllers/hr/worker_fields_controller.rb:17
  Hr::WorkerField Create (0.3ms)  INSERT INTO `hr_worker_fields` (`field_name`, `created_at`, `updated_at`) VALUES ('xxx', '2020-07-13 19:24:02', '2020-07-13 19:24:02')
  ↳ app/controllers/hr/worker_fields_controller.rb:17
   (3.8ms)  COMMIT
  ↳ app/controllers/hr/worker_fields_controller.rb:17
Completed 500  in 16ms (ActiveRecord: 4.9ms)



ActionController::UnknownFormat - ActionController::UnknownFormat:
  app/controllers/hr/worker_fields_controller.rb:18:in `create'

Started POST "/__better_errors/39432d2893672a62/variables" for ::1 at 2020-07-13 19:24:02 +0800

也网上看了些别人的情况,在我这没啥用。

还试着在表单加 format::js 的参数

实在不知道解决方案

日志贴全

去掉

respond_to do |format|
   format.js
 end
Rei 回复

感谢提醒,刚重新编辑了一下

huacnlee 回复

谢谢帮忙;但是不太行报了新错

Processing by Hr::WorkerFieldsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"fTZiZhfkuxYwMC7+vUeOM0dcEuPslmTLHbJD5k/mJJn/2pTWa4gpClxEATxQsX/hh+Xh+lbfqvuuSEKoDxfrfg==", "hr_worker_field"=>{"field_name"=>"xxx"}}
  Fdn::User Load (0.6ms)  SELECT  `fdn_users`.* FROM `fdn_users` WHERE `fdn_users`.`id` = 1 LIMIT 1
  ↳ app/helpers/fdn/user_sessions_helper.rb:22
  Fdn::UserInformation Load (0.8ms)  SELECT `fdn_user_informations`.* FROM `fdn_user_informations` WHERE `fdn_user_informations`.`user_id` = 1
  ↳ app/helpers/fdn/user_sessions_helper.rb:22
   (0.2ms)  BEGIN
  ↳ app/controllers/hr/worker_fields_controller.rb:17
  Hr::WorkerField Create (0.4ms)  INSERT INTO `hr_worker_fields` (`field_name`, `created_at`, `updated_at`) VALUES ('xxx', '2020-07-13 19:28:25', '2020-07-13 19:28:25')
  ↳ app/controllers/hr/worker_fields_controller.rb:17
   (3.6ms)  COMMIT
  ↳ app/controllers/hr/worker_fields_controller.rb:17
Completed 500  in 41ms (ActiveRecord: 5.6ms)



ActionController::UnknownFormat - Hr::WorkerFieldsController#create is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []:

Started POST "/__better_errors/fd9a32653680483b/variables" for ::1 at 2020-07-13 19:28:25 +0800

Processing by Hr::WorkerFieldsController#create as HTML

显示提交的还是 HTML,js 里面有没有加入 ujs?

Rei 回复

请问是这个? application.js

//= require jquery2
//= require jquery_ujs
//= require jquery-ui
SunA0 回复

是的,似乎加了。

把页面渲染的表单 html 贴上来看看。

Rei 回复

谢谢

<form class="navbar-left" id="new_hr_worker_field" max_columns="2" action="/hr/worker_fields" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="fTZiZhfkuxYwMC7+vUeOM0dcEuPslmTLHbJD5k/mJJn/2pTWa4gpClxEATxQsX/hh+Xh+lbfqvuuSEKoDxfrfg==">
  <input id="tag_name" class="form-control" placeholder="标签名" type="text" name="hr_worker_field[field_name]">
  <input type="submit" name="commit" value="创建" class="btn btn-primary " data-disable-with="创建">
</form>

看不出问题……

在页面加这个 button,点击看看有无确认框,测试 ujs 是否有工作,浏览器 console 有无异常。

<button type="button" data-confirm="work?">test</button>
10楼 已删除
SunA0 关闭了讨论 07月14日 10:27
SunA0 重新开启了讨论 07月14日 10:27
SunA0 关闭了讨论 07月14日 10:28
SunA0 重新开启了讨论 07月14日 10:33
SunA0 关闭了讨论 07月14日 10:43
SunA0 重新开启了讨论 07月14日 11:08
Rei 回复

谢谢帮助!

点开这个按钮 就不行

<button class="btn btn btn-primary btn-xs" title="新增" type="button" onclick="window.location='/hr/worker_fields/new';"><i class="fa fa-plus"></i>&nbsp;新增</button>

正常写 link 就可以

<a href="/hr/worker_fields/new">new</a>

还挺神奇

SunA0 回复

这是整个 js 执行有问题了吧?打开调试器看 js 是不是报异常,有没有禁用 js?

SunA0 关闭了讨论 07月16日 09:48
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册