简体   繁体   中英

ActionController::UnknownFormat in Devise::RegistrationsController#create due to CRUD implementation of Users

Intention

I have created a User model as the Devise documentation indicates. Later on, I have scaffolded a controller and views so to have an index and a visual interface to handle my Users.

Problem

After visiting edit_user_path , I submit a new user to the database, SQLite adds the record (so the information is being saved), but something happens and I am not redirected to the root directory, but get an exception instead:

ActionController::UnknownFormat in Devise::RegistrationsController#create

How can I fix this exception and make Rails flow to the root_path?

Here I add you the files so you can discern what's going on:

routes.rb

  #RESTful routes
  devise_for :users
  resources :users, :except => [:create]

  resources :clans
  resources :games
  resources :matches

  #Root and debug
  root :to => 'debug#features'

rake routes

                  Prefix Verb   URI Pattern           Controller#Action

        new_user_session GET    /users/sign_in(.:format)   devise/sessions#new
            user_session POST   /users/sign_in(.:format)   devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)   evise/sessions#destroy
           user_password POST   /users/password(.:format)   devise/passwords#create
       new_user_password GET    /users/password/new(.:format) devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   users GET    /users(.:format)               users#index
                         POST   /users(.:format)               users#create
                new_user GET    /users/new(.:format)           users#new
               edit_user GET    /users/:id/edit(.:format)      users#edit
                    user GET    /users/:id(.:format)           users#show
                         PATCH  /users/:id(.:format)           users#update
                         PUT    /users/:id(.:format)           users#update
                         DELETE /users/:id(.:format)           users#destroy
                   clans GET    /clans(.:format)               clans#index
                         POST   /clans(.:format)               clans#create
                new_clan GET    /clans/new(.:format)           clans#new
               edit_clan GET    /clans/:id/edit(.:format)      clans#edit
                    clan GET    /clans/:id(.:format)           clans#show
                         PATCH  /clans/:id(.:format)           clans#update
                         PUT    /clans/:id(.:format)           clans#update
                         DELETE /clans/:id(.:format)           clans#destroy
                   games GET    /games(.:format)               games#index
                         POST   /games(.:format)               games#create
                new_game GET    /games/new(.:format)           games#new
               edit_game GET    /games/:id/edit(.:format)      games#edit
                    game GET    /games/:id(.:format)           games#show
                         PATCH  /games/:id(.:format)           games#update
                         PUT    /games/:id(.:format)           games#update
                         DELETE /games/:id(.:format)           games#destroy
                 matches GET    /matches(.:format)             matches#index
                         POST   /matches(.:format)             matches#create
               new_match GET    /matches/new(.:format)         matches#new
              edit_match GET    /matches/:id/edit(.:format)    matches#edit
                   match GET    /matches/:id(.:format)         matches#show
                         PATCH  /matches/:id(.:format)         matches#update
                         PUT    /matches/:id(.:format)         matches#update
                         DELETE /matches/:id(.:format)         matches#destroy
                    root GET    /                              debug#features
          debug_features GET    /debug/features(.:format)      debug#features

edit_user_path: render devise/registrations/new.html.erb: form_for

form_for(resource, as: resource_name, url: user_registration_path(resource_name))

POST & SQL as shown in cmd (Windows 8)

Started POST "/users.user" for 127.0.0.1 at 2015-12-17 21:18:41 +0100
Processing by Devise::RegistrationsController#create as
  Parameters: {"utf8"=>"V", "authenticity_token"=>"EUP/Aylqxv7fKSe4Rm37IO09bgfar
ayFcoaLsKiF9zC7vUxgtwciM/TgDfCuLAr1NecxTgJukDGTRQ7L18AkGg==", "user"=>{"name"=>"
aaa", "email"=>"aaa@aaa.aaa", "password"=>"[FILTERED]", "password_confirmation"=
>"[FILTERED]", "city"=>"Valencia", "country"=>"", "birth"=>"", "about"=>"", "lik
es"=>"da", "style"=>"asd", "steam"=>"", "psn"=>"", "xbox"=>"", "twitter"=>""}, "
commit"=>"Sign up"}
   (0.0ms)  begin transaction
  User Exists (0.0ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'aa
a@aaa.aaa' LIMIT 1
  SQL (1.0ms)  INSERT INTO "users" ("email", "encrypted_password", "name", "city
", "country", "about", "likes", "style", "steam", "psn", "xbox", "twitter", "cre
ated_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["em
ail", "aaa@aaa.aaa"], ["encrypted_password", "$2a$10$nRaRhwdq3h9bYDdDcoGbIOfirQp
6jHygl55vvhhaLePN7WET6N0xG"], ["name", "aaa"], ["city", "Valencia"], ["country",
 ""], ["about", ""], ["likes", "da"], ["style", "asd"], ["steam", ""], ["psn", "
"], ["xbox", ""], ["twitter", ""], ["created_at", "2015-12-17 20:18:41.522714"],
 ["updated_at", "2015-12-17 20:18:41.522714"]]
   (437.6ms)  commit transaction
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDE
R BY "users"."id" ASC LIMIT 1  [["id", 1]]
   (0.0ms)  begin transaction
  SQL (1.0ms)  UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" =
?, "last_sign_in_ip" = ?, "current_sign_in_ip" = ?, "sign_in_count" = ?, "update
d_at" = ? WHERE "users"."id" = ?  [["last_sign_in_at", "2015-12-17 20:18:41.9973
08"], ["current_sign_in_at", "2015-12-17 20:18:41.997308"], ["last_sign_in_ip",
"127.0.0.1"], ["current_sign_in_ip", "127.0.0.1"], ["sign_in_count", 1], ["updat
ed_at", "2015-12-17 20:18:41.999315"], ["id", 5]]
   (117.0ms)  commit transaction
Completed 406 Not Acceptable in 756ms (ActiveRecord: 557.6ms)

Traceback

responders (2.1.0) lib/action_controller/respond_with.rb:205:in `respond_with'
devise (3.5.3) app/controllers/devise/registrations_controller.rb:23:in `create'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:137:in `process'

respond_with.rb

def respond_with(*resources, &block)
  if self.class.mimes_for_respond_to.empty?
    raise "In order to use respond_with, first you need to declare the " \
      "formats your controller responds to in the class level."
  end

  mimes = collect_mimes_from_class_level()
  collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
  block.call(collector) if block_given?

  if format = collector.negotiate_format(request)
    _process_format(format)
    options = resources.size == 1 ? {} : resources.extract_options!
    options = options.clone
    options[:default_response] = collector.response
    (options.delete(:responder) || self.class.responder).call(self, resources, options)
  else
    raise ActionController::UnknownFormat #<-----------line 205
  end
end

aplication_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :name, :city, :country, :birth, :about, :likes, :style, :steam, :psn, :xbox, :twitter) }
  end
end

While you get ActionController::UnknownFormat in Devise::RegistrationsController#create in devise, most of the problems is in your registration form, remove resource_name in your url part

user_registration_path does not requires an additional parameter, and you pass the resource_name to it, which is equal to user in your case. It gets interpreted as the format, and therefore you get url like http://localhost:3000/users.user (user is set as a format in this case). MORE

Before

form_for(resource, as: resource_name, url: user_registration_path(resource_name))

After

form_for(resource, as: resource_name, url: user_registration_path)

I don't know if is related to your problem, but you have some duplicate (or better useless) routes and maybe action in your Controllers.

According to the result of bin/rake routes

cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   users GET    /users(.:format)               users#index
                         POST   /users(.:format)               users#create # <-- ?
                new_user GET    /users/new(.:format)           users#new  # <-- ?
               edit_user GET    /users/:id/edit(.:format)      users#edit  # <-- ?
                    user GET    /users/:id(.:format)           users#show
                         PATCH  /users/:id(.:format)           users#update  # <-- ?
                         PUT    /users/:id(.:format)           users#update  # <-- ?
                         DELETE /users/:id(.:format)           users#destroy  # <-- ?

you already manage the new , create , edit , update and destroy action on User resource with Devise RegistrationController , but there are also similar action listed under /users/... path, and maybe related action in UsersController .

To keep clean your code and avoid misunderstandings is better if you keep only the useful action in the UsersControllers : index and show .

Try to change your routes.rb file:

devise_for :users
resources :users, only: [:index, :show]

respond_to :html, :json添加到application_controller.rb解决了这个问题

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