表单值未通过复选框Rails 4提交

时间:2015-06-18 16:37:27

标签: ruby-on-rails forms ruby-on-rails-4 checkbox

我有许多复选框,这些复选框基于每个用户的角色,具有多对多的关系。但是当我尝试提供这些角色时,它似乎没有得到保存,尽管表单没有返回任何错误。当我在控制台中尝试它时,User.find(1).role_ids = 3它会被保存并在网页上查看时返回复选框。 我是Rails的新手,我需要一点帮助来解决这个问题。

我的.html.erb

<%= form_for(:user, url: {action: 'update', id: @user.id}) do |f| %>
<tr>
    <% for role in Role.all %>
    <td><%= check_box "user[role_ids][]", role.id, @user.roles.include?(role) %></td><td></td><td></td><td></td><td></td><td></td><td></td>
    <% end %>
</tr>

我的controller.rb

    def edit 
        @user = User.find(params[:id])
      end
    def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:notice] = "Role assigned successfully" 
      redirect_to(:action => 'show', :id => @user.id)
    else
      render('edit')
    end
  end

我的日志文件:

Started GET "/users/edit/11" for ::1 at 2015-06-18 23:42:49 +0530
  [1m[36mActiveRecord::SchemaMigration Load (0.0ms)[0m  [1mSELECT `schema_migrations`.* FROM `schema_migrations`[0m
Processing by UsersController#edit as HTML
  Parameters: {"id"=>"11"}
  [1m[35mUser Load (0.0ms)[0m  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 11  ORDER BY `users`.`id` ASC LIMIT 1
  [1m[36mUser Load (0.0ms)[0m  [1mSELECT  `users`.* FROM `users` WHERE `users`.`id` = 11 LIMIT 1[0m
  [1m[35mRole Load (0.0ms)[0m  SELECT `roles`.* FROM `roles`
  [1m[36m (1.0ms)[0m  [1mSELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 11[0m
  [1m[35mCACHE (0.0ms)[0m  SELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 11  [["user_id", 11]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 11[0m  [["user_id", 11]]
  [1m[35mCACHE (0.0ms)[0m  SELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 11  [["user_id", 11]]
  Rendered users/edit.html.erb within layouts/application (66.0ms)
Completed 200 OK in 305ms (Views: 243.2ms | ActiveRecord: 21.0ms)


Started GET "/assets/home.self-b60acab5af8d245ed2b3012f77d11d8d207d8eada27756badaf4ef05770d60f6.css?body=1" for ::1 at 2015-06-18 23:42:49 +0530


Started GET "/assets/three_datas.self-b60acab5af8d245ed2b3012f77d11d8d207d8eada27756badaf4ef05770d60f6.css?body=1" for ::1 at 2015-06-18 23:42:49 +0530


Started GET "/assets/threeddata.self-b60acab5af8d245ed2b3012f77d11d8d207d8eada27756badaf4ef05770d60f6.css?body=1" for ::1 at 2015-06-18 23:42:49 +0530


Started GET "/assets/users.self-b60acab5af8d245ed2b3012f77d11d8d207d8eada27756badaf4ef05770d60f6.css?body=1" for ::1 at 2015-06-18 23:42:49 +0530


Started GET "/assets/application.self-517077f606e4f2083e00ef48850c40b9284304d56f4df49218b1d4fc71083e80.css?body=1" for ::1 at 2015-06-18 23:42:49 +0530


Started GET "/javascripts/public.js" for ::1 at 2015-06-18 23:42:49 +0530

ActionController::RoutingError (No route matches [GET] "/javascripts/public.js"):
  actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.1) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.1) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.1) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.1) lib/rails/application.rb:164:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  rack (1.6.1) lib/rack/content_length.rb:15:in `call'
  rack (1.6.1) lib/rack/handler/webrick.rb:89:in `service'
  C:/Ruby21/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  C:/Ruby21/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  C:/Ruby21/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'


  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_route.html.erb (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_table.html.erb (12.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (55.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (26.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (55.0ms)


Started POST "/users/update/11" for ::1 at 2015-06-18 23:42:53 +0530
Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"B9vvbQhZJe1H4u9T2XQsIHH14ysoe6oHOAnLt5fKwAOMPBheb/IEHk2Bevqe6Q1unoNGo0IAHQt47WhMI2Uybw==", "user"=>{"role_ids"=>["2", "3"]}, "commit"=>"Update Role", "id"=>"11"}
  [1m[36mUser Load (1.0ms)[0m  [1mSELECT  `users`.* FROM `users` WHERE `users`.`id` = 11  ORDER BY `users`.`id` ASC LIMIT 1[0m
  [1m[35mUser Load (0.0ms)[0m  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 11 LIMIT 1
Unpermitted parameter: role_ids
  [1m[36m (0.0ms)[0m  [1mBEGIN[0m
  [1m[35m (0.0ms)[0m  COMMIT
Redirected to http://localhost:3000/users/index?id=11
Completed 302 Found in 11ms (ActiveRecord: 1.0ms)


Started GET "/users/index?id=11" for ::1 at 2015-06-18 23:42:53 +0530
Processing by UsersController#index as HTML
  Parameters: {"id"=>"11"}
  [1m[36mUser Load (1.0ms)[0m  [1mSELECT  `users`.* FROM `users` WHERE `users`.`id` = 11  ORDER BY `users`.`id` ASC LIMIT 1[0m
  [1m[35mUser Load (0.0ms)[0m  SELECT `users`.* FROM `users`  ORDER BY users.id ASC
  Rendered users/index.html.erb within layouts/application (4.0ms)
Completed 200 OK in 31ms (Views: 29.0ms | ActiveRecord: 1.0ms)


Started GET "/javascripts/public.js" for ::1 at 2015-06-18 23:42:53 +0530

ActionController::RoutingError (No route matches [GET] "/javascripts/public.js"):
  actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.1) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.1) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.1) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.1) lib/rails/application.rb:164:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  rack (1.6.1) lib/rack/content_length.rb:15:in `call'
  rack (1.6.1) lib/rack/handler/webrick.rb:89:in `service'
  C:/Ruby21/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  C:/Ruby21/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  C:/Ruby21/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'


  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (40.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (26.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (57.0ms)


Started GET "/users/edit/11" for ::1 at 2015-06-18 23:42:58 +0530
Processing by UsersController#edit as HTML
  Parameters: {"id"=>"11"}
  [1m[36mUser Load (0.0ms)[0m  [1mSELECT  `users`.* FROM `users` WHERE `users`.`id` = 11  ORDER BY `users`.`id` ASC LIMIT 1[0m
  [1m[35mUser Load (0.0ms)[0m  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 11 LIMIT 1
  [1m[36mRole Load (0.0ms)[0m  [1mSELECT `roles`.* FROM `roles`[0m
  [1m[35m (0.0ms)[0m  SELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 11
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 11[0m  [["user_id", 11]]
  [1m[35mCACHE (0.0ms)[0m  SELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 11  [["user_id", 11]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT `roles`.id FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 11[0m  [["user_id", 11]]
  Rendered users/edit.html.erb within layouts/application (5.0ms)
Completed 200 OK in 36ms (Views: 33.0ms | ActiveRecord: 0.0ms)


Started GET "/javascripts/public.js" for ::1 at 2015-06-18 23:42:58 +0530

ActionController::RoutingError (No route matches [GET] "/javascripts/public.js"):
  actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.1) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.1) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.1) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.1) lib/rails/application.rb:164:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  rack (1.6.1) lib/rack/content_length.rb:15:in `call'
  rack (1.6.1) lib/rack/handler/webrick.rb:89:in `service'
  C:/Ruby21/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  C:/Ruby21/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  C:/Ruby21/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'


  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_table.html.erb (2.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (40.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (26.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
  Rendered C:/Ruby21/lib/ruby/gems/2.1.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (54.0ms)

1 个答案:

答案 0 :(得分:0)

与Rails 4一样,输入需要列入白名单。将日志文件中显示的未经许可的参数[]添加到相应的控制器就可以了。 accepted_nested_attributes_for也是必需的,以便当前模型通过当前模型接受其他模型的嵌套属性。