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

时间:2015-12-17 21:05:25

标签: ruby-on-rails ruby devise ruby-on-rails-4.2 respond-with

意图

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

问题

在访问 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:render devise / registrations / new.html.erb:form_for

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

POST& SQL如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)

回溯

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

3 个答案:

答案 0 :(得分: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  # <-- ?

您已使用设计{{1}管理new资源上的createeditupdatedestroyUser操作},但RegistrationController路径下列出了类似的操作,也可能是/users/...中的相关操作。

如果只保留UsersControllerUsersControllersindex中的有用操作,那么保持代码清洁并避免误解会更好。

尝试更改show文件:

routes.rb

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在设计中得到var len = tpj(".custom-social-proof").length; var random = Math.floor( Math.random() * len ) + 1; setInterval(function(){ tpj(".custom-social-proof").eq(random).stop().slideToggle('slow'); }, 8000); tpj(".custom-close").click(function() { tpj(".custom-social-proof").stop().slideToggle('slow'); }); 的同时,大部分问题出在您的注册表中,请在网址部分中删除resource_name

  

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

之前

ActionController::UnknownFormat in Devise::RegistrationsController#create

之后

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