remote:用于执行新操作的form_for的true选项,但不是create action

时间:2014-03-20 21:21:44

标签: jquery ruby-on-rails ajax ruby-on-rails-4

更新

这已经解决了!这是一个困难的,因为它根本不容易调试。但是注意到我在表单标签中有一个表单标签,我亲切的“回答者”指出这需要修复,无论这是否是问题的根本原因。请记住,我同意他nested attributes绝对是继续这个问题的方法,但是因为这是一个学校项目,我选择将内部形式移到外部形式之外现在要做到我所期望的。这涉及到更改下面看到的一些代码。但我不会发布新代码,因为我同意nested attributes是这种情况下的方法。当我有空的时候,这将成为我这个项目的下一个目标。

原始问题:

我正在使用Rails 4,我有这样的嵌套资源:

resources :data do
  resources :todo_items
end

datum对象的编辑页面中,我打算让用户能够通过ajax将todo_item添加到datum对象。当您单击“添加待办事项”按钮时,我的表单用于创建新的todo_item,并且在填写必填字段后,用户可以单击添加,我打算再次发出请求这将显示todo_item部分,其中包含一个复选框,供用户显示其是否完整。新表单很好,但是当我单击添加按钮以显示新创建的ActionController::UnknownFormat时,我收到todo_item错误。这是我正在使用的代码:

应用程序/视图/数据/ _form.html.erb

...
<%= link_to 'Add ToDo', new_datum_todo_item_path(@datum.id), remote: true %>
...

应用程序/控制器/ todo_items_controller.rb

class TodoItemsController < ApplicationController
  def new
    @datum = Datum.find(params[:datum_id])
    @todo = TodoItem.new
    respond_to do |format|
      format.js
    end
  end

  def create
    @datum = Datum.find(params[:datum_id])
    @todo = @datum.todo_items.create!(todo_item_params)
    respond_to do |format|
      format.js
    end
  end

  private

    def todo_item_params
      params.require(:todo_item).permit(:task, :content, :done)
    end
end

app / views / todo_items / _form.html.erb('新'表单)

<div id="<%= datum.id %>-<%= todo.id %>" class="panel todo-item">
  <%= bootstrap_form_for([datum, todo], remote: true) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <%= f.hidden_field :done, value: false %>
    <%= f.text_field :task, hide_label: true, placeholder: 'task' %>
    <%= f.text_area :content, hide_label: true, placeholder: 'content' %>
    <%= f.submit 'Add', class: 'btn btn-primary btn-sm' %>
  <% end %>
</div>

应用程序/视图/ todo_items / _todo.html.erb

<%= bootstrap_form_for([datum, todo], remote: true) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.check_box :done, hide_label: true %>
  <div><%= todo.task %></div>
  <div><%= todo.content %></div>
<% end %>

app / views / todo_items / new.js.erb(正在运行的响应)

$('.panel-body').append("<%= escape_javascript(render partial: 'form', locals: { datum: @datum, todo: @todo }) %>");

app / views / todo_items / create.js.erb(无效的响应)

$("#<%= @datum.id %>-<%= @todo.id %>").html("<%= escape_javascript(render partial: 'todo_items/todo', locals: { datum: @datum, todo: @todo }) %>");

有人可以帮我弄清楚我在这里做错了什么吗?我只打算respond_to format.js。两种形式都包括remote: true。我觉得我在这里错过了一些非常愚蠢的东西,但我现在已经试图解决这个问题很长一段时间了。谢谢!如果我需要提供任何进一步的信息,请告诉我......

更新

以下是我的服务器日志中显示的新请求和创建请求:

Started GET "/data/532799016a616b1786040000/todo_items/new" for 127.0.0.1 at 2014-03-20 16:05:04 -0600
Processing by TodoItemsController#new as JS
  Parameters: {"datum_id"=>"532799016a616b1786040000"}
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.4793ms
  MOPED: 127.0.0.1:27017 QUERY        database=datums_app_development collection=data selector={"_id"=>BSON::ObjectId('532799016a616b1786040000')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.4106ms
  Rendered shared/_error_messages.html.erb (0.6ms)
  Rendered todo_items/_form.html.erb (8.8ms)
  Rendered todo_items/new.js.erb (14.3ms)
Completed 200 OK in 39ms (Views: 15.8ms | ActiveRecord: 0.0ms)


Started POST "/data/532799016a616b1786040000/todo_items" for 127.0.0.1 at 2014-03-20 16:05:11 -0600
Processing by TodoItemsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"stsyxQfAuzPa1BGnOx2cAuagZqFuSHF6jfos9/euw94=", "todo_item"=>{"done"=>"false", "task"=>"Task", "content"=>"Content"}, "commit"=>"Add", "datum_id"=>"532799016a616b1786040000"}
  MOPED: 127.0.0.1:27017 QUERY        database=datums_app_development collection=data selector={"_id"=>BSON::ObjectId('532799016a616b1786040000')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.5380ms
  MOPED: 127.0.0.1:27017 UPDATE       database=datums_app_development collection=data selector={"_id"=>BSON::ObjectId('532799016a616b1786040000')} update={"$push"=>{"todo_items"=>{"_id"=>BSON::ObjectId('532b66176a616b4160010000'), "task"=>"Task", "content"=>"Content", "done"=>false, "updated_at"=>2014-03-20 22:05:11 UTC, "created_at"=>2014-03-20 22:05:11 UTC}}} flags=[]
                         COMMAND      database=datums_app_development command={:getlasterror=>1, :w=>1} runtime: 1.1161ms
Completed 406 Not Acceptable in 8ms

ActionController::UnknownFormat - ActionController::UnknownFormat:
  actionpack (4.0.2) lib/action_controller/metal/mime_responds.rb:372:in `retrieve_collector_from_mimes'
  actionpack (4.0.2) lib/action_controller/metal/mime_responds.rb:189:in `respond_to'
  app/controllers/todo_items_controller.rb:13:in `create'
  actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.0.2) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.0.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.0.2) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (4.0.2) lib/active_support/callbacks.rb:403:in `_run__1881092772621756057__process_action__callbacks'
  activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.2) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (4.0.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.0.2) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.0.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.0.2) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.0.2) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
  activerecord (4.0.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.0.2) lib/abstract_controller/base.rb:136:in `process'
  actionpack (4.0.2) lib/abstract_controller/rendering.rb:44:in `process'
  actionpack (4.0.2) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.0.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.0.2) lib/action_controller/metal.rb:231:in `block in action'
  actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:48:in `call'
  actionpack (4.0.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:680:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  activerecord (4.0.2) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.2) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.2) lib/active_support/callbacks.rb:373:in `_run__936815074203488114__call__callbacks'
  activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (1.0.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (1.0.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (1.0.1) lib/better_errors/middleware.rb:56:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.2) lib/rails/engine.rb:511:in `call'
  railties (4.0.2) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /home/jake/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /home/jake/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /home/jake/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

3 个答案:

答案 0 :(得分:1)

一个可能的错误与dom对象选择器有关。试试:

$('#<%= @datum.id %>-<%= @todo.id %>').html("<%= escape_javascript(render partial: 'todo_items/todo', locals: { datum: @datum, todo: @todo }) %>");

jquery使用#id选择器而不是.class。由于您已将实例信息(id="<%= datum.id %>-<%= todo.id %>")添加到id属性:

<div id="<%= datum.id %>-<%= todo.id %>" class="panel todo-item">

审核评论中链接的源代码后更新

您在form内使用了form这是不正确的,浏览器不支持,可能会导致不可预测的行为,例如您的情况。

您应该改变一下您的方法/实施。在这种情况下,您可能需要使用accepts_nested_attributes_forfields_for

Here a related RailsCast

答案 1 :(得分:1)

我认为铁路的remote: true选项是&#34;黑魔法&#34;我从不使用它 - 它比它的价值更麻烦。你的问题就是一个很好的例子。

如果我在哪里,我会使用常规的javascript而不是使用特定的rails位。它更详细,但也更清晰,更容易调试。

但是,我不能为你编程。

祝你好运!

答案 2 :(得分:-1)

关键在这里 Processing by TodoItemsController#create as HTML。在您的创建操作中,您只响应js mime-type,但请求正在以html格式处理。因此未知格式错误。将format.html添加到操作中,或者您可以通过向表单添加format: :js来强制它为js mime类型。