我正在学习Rails(所以这可能是一个愚蠢的错误)。我正在尝试编写一个同时更新父级及其子级的编辑屏幕。当我点击保存时,我得到:
NoMethodError in TasksController#update
undefined method `to_sym' for #<Arel::Attributes::Integer:0x00000004d11970>
我无法弄清楚是什么导致了这个以及这个整数对象在哪里/是什么。任何人都可以给我任何指示吗?
编辑:如果我从头开始使用新的Rails项目,我可以完成这项工作。引发错误的参数是关键字“flactions_attributes”下的参数。似乎缺少一些值,但这不是问题。
父模型是Task,子模型是Flaction。 (从我这里拿走它,你不想在Rails中命名一个表'action')。
错误页面也有助于显示params哈希(请原谅我的'喜剧'测试数据。):
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"QS1gVSz+LKNIyBsIdnSMVfciBA+3j0nFK37477A7mps=",
"task"=>{"name"=>"Finish secret lair",
"description"=>"",
"flactions_attributes"=>{"0"=>{"name"=>"Choose a colour scheme",
"notes"=>"Ask The Voices?",
"id"=>"80"},
"1"=>{"name"=>"Pick a name",
"notes"=>"Something with skulls in it?",
"id"=>"81"}}},
"x"=>"3",
"y"=>"14",
"id"=>"53"}
这是控制器中的更新方法。 AFAICS完全是香草:
def update
@task = Task.find(params[:id])
respond_to do |format|
if @task.update_attributes(params[:task])
format.html { redirect_to(@task, :notice => 'Task was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
end
end
end
所以:@ task.update_attributes找到一个整数并尝试在其上运行to_sym。我传递给update_attributes的唯一东西是params,并且那里没有整数键。我如何弄清楚发生了什么?
这是_form文件,FWIW:
<%= form_for(@task) do |f| %>
<% if @task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% @task.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="<%= @task.hold ? 'task held' : 'task' %>" >
<div class="field name">
<%= f.text_field :name, size: 20 %>
<span class="buttons">
<%= if @task.hold
link_to image_tag('unhold-icon.png', alt: 'Un-hold'), change_hold_task_path(@task)
else
link_to image_tag('hold-icon.png', alt: 'Hold'), change_hold_task_path(@task)
end %>
<%= link_to image_tag('delete-icon.png', alt: 'Delete'), @task, confirm: 'Are you sure?', method: :delete %>
</span>
</div>
<div class="field desc">
<%= f.text_area :description, rows: 3, cols: 95 %>
</div>
<table>
<% f.fields_for :flactions do |a| %>
<tr>
<td class="field"> <%= a.text_field :name %> </td>
<td class="field"> <%= a.text_area :notes, rows: 2, columns: 40 %> </td>
<td class="buttons">
<%= link_to image_tag("up-icon-small.png", alt: 'Move Up'), move_flaction_path(a.object, direction: "up") %>
<%= link_to image_tag("down-icon-small.png", alt: 'Move Down'), move_flaction_path(a.object, direction: "down") %>
<%= link_to image_tag("delete-icon-small.png", alt: 'Delete'), a.object, :confirm => 'Are you sure?', :method => :delete %>
</td>
</tr>
<% end # f.fields_for %>
</table>
<%= image_submit_tag('save-icon.png', alt: 'Save') %>
</div> <!-- of div.task or div.task_held -->
<% end %>
编辑:根据要求进行堆栈跟踪:
activerecord (3.0.8) lib/active_record/base.rb:1267:in `block in expand_hash_conditions_for_aggregates'
activerecord (3.0.8) lib/active_record/base.rb:1266:in `each'
activerecord (3.0.8) lib/active_record/base.rb:1266:in `expand_hash_conditions_for_aggregates'
activerecord (3.0.8) lib/active_record/relation/query_methods.rb:225:in `build_where'
activerecord (3.0.8) lib/active_record/relation/query_methods.rb:76:in `where'
activerecord (3.0.8) lib/active_record/relation/spawn_methods.rb:124:in `apply_finder_options'
activerecord (3.0.8) lib/active_record/relation/finder_methods.rb:143:in `all'
activerecord (3.0.8) lib/active_record/nested_attributes.rb:364:in `assign_nested_attributes_for_collection_association'
activerecord (3.0.8) lib/active_record/nested_attributes.rb:254:in `flactions_attributes='
activerecord (3.0.8) lib/active_record/base.rb:1564:in `block in attributes='
activerecord (3.0.8) lib/active_record/base.rb:1560:in `each'
activerecord (3.0.8) lib/active_record/base.rb:1560:in `attributes='
activerecord (3.0.8) lib/active_record/persistence.rb:131:in `block in update_attributes'
activerecord (3.0.8) lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
activerecord (3.0.8) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
activerecord (3.0.8) lib/active_record/transactions.rb:207:in `transaction'
activerecord (3.0.8) lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
activerecord (3.0.8) lib/active_record/persistence.rb:130:in `update_attributes'
app/controllers/tasks_controller.rb:62:in `block in update'
actionpack (3.0.8) lib/action_controller/metal/mime_responds.rb:264:in `call'
actionpack (3.0.8) lib/action_controller/metal/mime_responds.rb:264:in `retrieve_response_from_mimes'
actionpack (3.0.8) lib/action_controller/metal/mime_responds.rb:191:in `respond_to'
app/controllers/tasks_controller.rb:61:in `update'
actionpack (3.0.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.8) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.8) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.8) lib/active_support/callbacks.rb:436:in `_run__950750868564864989__process_action__1833593346352443574__callbacks'
activesupport (3.0.8) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.8) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.8) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.8) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.8) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.8) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.8) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.8) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.8) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.8) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.8) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.8) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.8) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.8) lib/action_dispatch/routing/route_set.rb:493:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.3) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.8) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.8) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.8) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.8) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.8) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.8) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.3) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.8) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.8) lib/rails/application.rb:168:in `call'
railties (3.0.8) lib/rails/application.rb:77:in `method_missing'
railties (3.0.8) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.3) lib/rack/content_length.rb:13:in `call'
rack (1.2.3) lib/rack/handler/webrick.rb:52:in `service'
/usr/lib/ruby/1.9.2-p180/webrick/httpserver.rb:111:in `service'
/usr/lib/ruby/1.9.2-p180/webrick/httpserver.rb:70:in `run'
/usr/lib/ruby/1.9.2-p180/webrick/server.rb:183:in `block in start_thread'
答案 0 :(得分:0)
GOT IT!这为迄今为止最不实用的Rails错误赢得了个人奖金。
问题是因为我在模型中编写了自己的Task.flactions方法。删除后,它工作正常。所以,还有一件事要记住永远不要再做了:不要覆盖Rails为你创建的方法。
顺便说一下,据我所知,目前记录的Rails 3中唯一的父母子窗体位于ri form_for
。那里也有例子。值得一看。