ruby-on-rails ActionController::Devise::RegistrationsController#中的未知格式由于用户的CRUD实现而创建

tkqqtvp1  于 2023-02-26  发布在  Ruby
关注(0)|答案(3)|浏览(129)
    • 意图**

我已经创建了一个用户模型,正如Devise文档所指出的那样。稍后,我搭建了一个控制器和视图,以便有一个索引和一个可视化界面来处理我的用户。

    • 问题**

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

ActionController::UnknownFormat in Devise::RegistrationsController#create
    • 如何修复此异常并使Rails流向root_path?**

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

    • 路由. 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
    • 编辑用户路径:提交设备/注册/新. html. erb:* * 的表单
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"=>"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'
    • 使用. 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
    • 应用程序控制器. 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
gijlo24d

gijlo24d1#

当你在devise中得到ActionController::UnknownFormat in Devise::RegistrationsController#create时,大多数问题都出在你的注册表中,删除url部分中的resource_name
user_registration_path不需要额外的参数,您将resource_name传递给它,在您的示例中,resource_name等于user,它被解释为格式,因此您将得到类似http://localhost:3000/users.user的url(在本例中,user被设置为格式)。MORE
之前

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

之后

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

pw136qt22#

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

y1aodyip

y1aodyip3#

我不知道是否与你的问题有关,但你有一些重复的(或更好的无用)路线,也许在你的控制器的行动。
根据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 RegistrationController管理了User资源上的newcreateeditupdatedestroy操作,但是在/users/...路径下还列出了类似的操作,并且可能在UsersController中列出了相关的操作。
为了保持代码整洁并避免误解,最好只保留UsersControllers中有用的操作:indexshow
尝试更改您的routes.rb文件:

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

相关问题