我遇到以下问题:我想创建一个带有复选框的网站,用户可以在其中选择特定任务的其他用户(例如Doodle克隆)。到目前为止,我已经看到了该视图,该视图显示了所有当前可用的用户,但是现在我想让用户选择其中一个正在“参加”任务。
我通过控制台测试了模型,到目前为止一切正常,但是我无法在与会者帮助表中获取参与布尔值。如果我像这样运行代码,我总是会得到一个“
TasksController中的NoMethodError#participate
#的未定义方法`find_by_user_id'-错误!
有任何想法吗?
楷模:
class Testuser < ActiveRecord::Base
has_many :attendees
has_many :tasks, :through => :attendees
end
class Task < ActiveRecord::Base
has_many :attendees
has_many :testusers, :through => :attendees
class Attendee < ActiveRecord::Base
belongs_to :task
belongs_to :testuser
end
schema.rb:
create_table "attendees", force: true do |t|
t.integer "task_id"
t.integer "testuser_id"
t.boolean "participate"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tasks", force: true do |t|
t.string "title"
t.text "text"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "task_date"
t.boolean "participate"
end
create_table "testusers", force: true do |t|
t.string "username"
t.datetime "created_at"
t.datetime "updated_at"
end
task_controller.rb
def select_user
@testuser = Testuser.all
end
def participate
# "user_checkbox"=>["4","6","8"], => example_output of the parameters
params[:user_checkbox].each do |check|
check_id = check
t = Attendee.find_by_user_id(check_id)
t.update_attribute(:participate, true)
@attendee = Attendee(where participate == true)
end
end
select_user.html.erb
<h1>Select Users</h1>
<%= form_tag("/tasks/participate/", :method => "post") do %>
<ul>
<% @testuser.each do |testuser| %>
<li> <%= check_box_tag "user_checkbox[]",testuser.id %> <%= testuser.username %></li>
<% end %>
</ul>
<%= submit_tag("Teilnehmer hinzufügen") %>
<% end %>
全栈跟踪:
activerecord (4.1.6) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
app/controllers/tasks_controller.rb:46:in `block in participate'
app/controllers/tasks_controller.rb:41:in `each'
app/controllers/tasks_controller.rb:41:in `participate'
actionpack (4.1.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.6) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.6) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.6) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.6) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.6) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.6) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678: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.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254: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.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.6) 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.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.6) lib/rails/engine.rb:514:in `call'
railties (4.1.6) lib/rails/application.rb:144: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'
/Users/xxx/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/xxx/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/xxx/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
最佳答案
您的数据库表attendees
没有user_id
字段:
create_table "attendees", force: true do |t|
t.integer "task_id"
t.integer "testuser_id"
t.boolean "participate"
t.datetime "created_at"
t.datetime "updated_at"
end
这就是
t = Attendee.find_by_user_id(check_id)
不起作用的原因。另外,您的代码非常奇怪,效率极低。您为每个记录运行sql
SELECT
。您为每个记录运行sql UPDATE
,而没有事务。同样,您在每个步骤上都重写@attendee
,最后只有最后一个。并且此语法不正确:Attendee(where participate == true)
。您可以像这样重写所有内容:# in case if you don't need to run validations and callbacks on each Attendee
# you can make all job by 1 SQL UPDATE
Attendee.where(testuser_id: params[:user_checkbox]).update_all(participate: true)
@attendees = Attendee.where(testuser_id: params[:user_checkbox])
# in case if you need to run validations and callbacks on each Attendee
# you pull all records by 1 query and then update each
@attendees = Attendee.where(testuser_id: params[:user_checkbox])
Attendee.transaction do
@attendees.each do |attendee|
attendee.update_attributes!(participate: true)
end
end