由于用户的CRUD实现,Devise :: RegistrationsController#create中的ActionController :: UnknownFormat

问题描述 投票:0回答:2

意图

我已经按照Devise文档所示创建了一个User模型。后来,我搭建了一个控制器和视图,以便有一个索引和一个可视界面来处理我的用户。

问题

访问edit_user_path后,我向数据库提交了一个新用户,SQLite添加了记录(因此正在保存信息),但是发生了一些事情,并且我没有重定向到根目录,而是得到了异常:

ActionController::UnknownFormat in Devise::RegistrationsController#create

我如何解决此异常并使Rails流到root_path?

我在这里向您添加文件,以便您了解正在发生的事情:

routes.rb

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

  resources :clans
  resources :games
  resources :matches

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

耙道

                  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:渲染设计/注册/new.html.erb:form_for

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

cmd中显示的POST和SQL(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"=>"[email protected]", "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
[email protected]' 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", "[email protected]"], ["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
ruby-on-rails ruby devise ruby-on-rails-4.2 respond-with
2个回答
1
投票

我不知道是否与您的问题有关,但是您的控制器中有一些重复的(或更好的无用的)路由,也可能有操作。

根据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  # <-- ?

您已经使用Devise new管理create资源对editupdatedestroyUserRegistrationController的操作,但是/users/...路径下也列出了类似的操作,以及UsersController中的相关动作。

如果仅在UsersControllersindexshow中保留有用的操作,则保持代码清洁并避免误解会更好。

尝试更改您的routes.rb文件:

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

0
投票

虽然您在设计中得到了ActionController::UnknownFormat in Devise::RegistrationsController#create,但大多数问题出在您的注册表中,请在URL部分中删除resource_name

user_registration_path不需要其他参数,您将resource_name传递给它,在您的情况下,它等于user。它被解释为格式,因此您会获得类似http://localhost:3000/users.user的网址(在这种情况下,用户被设置为格式)。 MORE

之前

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

之后

form_for(resource, as: resource_name, url: user_registration_path)
© www.soinside.com 2019 - 2024. All rights reserved.