简体   繁体   中英

Form value not submitted through checkbox Rails 4

I have a number of check-boxes based on roles of each user with a many-to-may relationship. But when I try to provide these roles, it does not seem to get saved, although the form returns no error. When I try it out in console, User.find(1).role_ids = 3 it gets saved and returns check-box ticked when viewed on webpage. I am new to Rails and I need a little help to get past this.

My .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>

My 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

My log file:

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)

As in Rails 4, the input need to be white-listed. Adding the unpermitted parameter[] as shown in the log file to the respective controller was all that did the trick. accepts_nested_attributes_for is also required so that the current model accepts the nested attributes of the other model through the current one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM