这个已经解决了!这很困难,因为调试起来并不容易。但是,在注意到我在表单标签中有一个表单标签时,我亲切的“回答者”指出,无论这是否是问题的根本原因,都必须对其进行修复。请记住,我同意他的观点,这nested attributes
绝对是继续进行下去的方法,但是由于这是目前的学校项目,因此我选择暂时将内部形式移至外部形式之外以使其完成我所期望的。这涉及更改您在下面看到的一些代码。但是,我不会发布该新代码,因为同样,nested attributes
在这种情况下,我同意这样做。当我有时间的时候,这将是我对该项目的下一个目标。
我正在使用Rails 4,并且已经嵌套了如下资源:
resources :data do
resources :todo_items
end
在datum
对象的编辑页面中,我打算让用户能够通过ajax 将todo_item
s 添加
到datum
对象。当您单击“添加待办事项”按钮时,将要求我创建新表单todo_item
,并在填写必填字段后,用户可以单击添加,并且我希望发出另一个请求,该请求将显示todo_item
为包含复选框的部分,供用户显示其是否完整。新表单显示正常,但是ActionController::UnknownFormat
单击添加按钮以显示新创建的时出现错误todo_item
。这是我正在使用的代码:
app / views / data / _form.html.erb
...
<%= link_to 'Add ToDo', new_datum_todo_item_path(@datum.id), remote: true %>
...
app / controllers / 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>
app / views / 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
。我觉得我在这里错过了一些非常愚蠢的东西,但是很长一段时间以来我一直在试图弄清这一点。谢谢!如果需要提供更多信息,请告诉我…
这是在服务器日志中出现的带有new和create请求的内容:
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'
一种可能的错误与dom对象选择器有关。尝试:
$('#<%= @datum.id %>-<%= @todo.id %>').html("<%= escape_javascript(render partial: 'todo_items/todo', locals: { datum: @datum, todo: @todo }) %>");
使用jquery
by #id
而不是by 选择器.class
。由于您已将实例信息(id="<%= datum.id %>-<%= todo.id %>"
)添加到id
属性:
<div id="<%= datum.id %>-<%= todo.id %>" class="panel todo-item">
评论后链接评论源代码后更新
您使用的是form
内部form
,这是不正确的,浏览器 不支持 它,并且可能会导致不可预测的行为,例如您的情况。
您应该改变您的方法/实现。在这种情况下,您可能需要使用accepts_nested_attributes_for
和fields_for
。
这里是相关的RailsCast。
create 当你刚起步或者只是想要测试一些东西时,倾向于从 create() 操作符入手。它接收一个有 observer 参数的函数。在前面的一些章节中已提及过,比如 Observable 包装章节。函数签名如下: Rx.Observable.create([fn]) 示例如下: Rx.Observable.create(observer => { observer.next( 1 );
Timer 创建一个Observable,它在一个给定的延迟后发射一个特殊的值。 Timer操作符创建一个在给定的时间段之后返回一个特殊值的Observable。 RxJava将这个操作符实现为timer函数。 timer返回一个Observable,它在延迟一段给定的时间后发射一个简单的数字0。 timer操作符默认在computation调度器上执行。有一个变体可以通过可选参数指定Schedu
Start 返回一个Observable,它发射一个类似于函数声明的值 编程语言有很多种方法可以从运算结果中获取值,它们的名字一般叫functions, futures, actions, callables, runnables等等。在Start目录下的这组操作符可以让它们表现得像Observable,因此它们可以在Observables调用链中与其它Observable搭配使用。 Start操
Repeat 创建一个发射特定数据重复多次的Observable Repeat重复地发射数据。某些实现允许你重复的发射某个数据序列,还有一些允许你限制重复的次数。 RxJava将这个操作符实现为repeat方法。它不是创建一个Observable,而是重复发射原始Observable的数据序列,这个序列或者是无限的,或者通过repeat(n)指定重复次数。 repeat操作符默认在trampoli
Range 创建一个发射特定整数序列的Observable Range操作符发射一个范围内的有序整数序列,你可以指定范围的起始和长度。 RxJava将这个操作符实现为range函数,它接受两个参数,一个是范围的起始值,一个是范围的数据的数目。如果你将第二个参数设为0,将导致Observable不发射任何数据(如果设置为负数,会抛异常)。 range默认不在任何特定的调度器上执行。有一个变体可以通过
Just 创建一个发射指定值的Observable Just将单个数据转换为发射那个数据的Observable。 Just类似于From,但是From会将数组或Iterable的数据取出然后逐个发射,而Just只是简单的原样发射,将数组或Iterable当做单个数据。 注意:如果你传递null给Just,它会返回一个发射null值的Observable。不要误认为它会返回一个空Observable