ruby-on-rails - Rails 4 Checkbox helper-table - Many_to_many

标签 ruby-on-rails ruby sqlite checkbox many-to-many

我遇到了以下问题:我想创建一个带有复选框的站点,用户可以在其中为某个任务选择其他用户(如涂鸦克隆)。到目前为止,我得到了一个 View ,它显示了所有当前可用的用户,但现在我想让用户选择其中哪一个正在“参加”任务。

我通过控制台测试了模型,到目前为止一切正常,但我无法在参加者助 watch 集中获得参与 bool 值。如果我像这样运行代码,我总是会得到一个“
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

架构.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

任务 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 field :

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

关于ruby-on-rails - Rails 4 Checkbox helper-table - Many_to_many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26591857/

相关文章:

android - 我在宠爱平台/硬件吗?

mysql - 应用程序和数据库UTF-8编码但仍然异常

ruby-on-rails - simple_form gem 中的 text_field_tag

ruby - 为什么 FakeFS 会破坏 RSpec?

android - 如何仅根据该记录的部分匹配返回记录?

mysql - SQLite 和 If() 条件

sql - Reddit 如何排序查询他们的数据库 "what' s hot”?

ruby-on-rails - 将新代码部署到 Heroku 时如何强制 ActionCable 断开所有客户端的连接

ruby - 如何查找并计算所有重复的 2/3/4 单词短语

ruby - 如何在单独的进程中运行代码?