Rails在简单表单提交时未找到Admins常量

时间:2015-02-03 00:29:32

标签: ruby-on-rails ruby-on-rails-4 devise simple-form uninitialized-constant

Rails不喜欢我的命名空间 - 即使我认为我遵守惯例。帮助

我正在使用simple_form来创建管理表单,在提交时,我收到错误:

  

未初始化的常量管理员

我的AdminsController看起来像这样:

class AdminsController < ApplicationController
  skip_before_action :authenticate_user!
  def index 
    @admins = Admin.all
  end

  def new
    @admin = Admin.new
  end

  def show
    @admin = Admin.find(params[:id])
  end

  def create

  end

  private 

  def admin_params
   params.require(:admin).permit(:username, :email, :password)
  end
end

我的管理模式 -

class Admin < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

我的表格是犹太人 - 视图如下:

<%= simple_form_for @admin do |f| %>
  <%= f.input :username, label: 'New Admin user name' %>
  <%= f.input :password, hint: 'No special characters. At leats 8 characters long.' %>
  <%= f.input :email, placeholder: 'admin@domain.com' %>
  <%= f.button :submit %>
<% end %>

并使用html中记录的正确路径进行渲染:

<form accept-charset="UTF-8" action="/admins" class="simple_form new_admin" id="new_admin" method="post" novalidate="novalidate">

Aaaaaand这与我想要的路线对齐,用rake路线进行双重检查:

ruby     POST /admins(.:format)admins #create

然而,当我提交时 - 路线未映射,并且rails无法找到我的控制器常量。我意识到我的创建动作是空白的 - 但这不是我刚才的问题。

我确信这很简单,我忽略了一些小事。 让我知道! 感谢

感谢您的提问 - 完整的回溯:

Completed 200 OK in 756ms (Views: 752.7ms | ActiveRecord: 0.5ms)


Started POST "/admins" for 127.0.0.1 at 2015-02-02 22:47:16 -0500

ActionController::RoutingError (uninitialized constant Admins):

activesupport (4.1.5) lib/active_support/inflector/methods.rb:238:in `const_get'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:238:in `block in constantize'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `each'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `inject'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `constantize'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:78:in `controller_reference'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:68:in `controller'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:46:in `call'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:45:in `call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.5) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.5) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.5) lib/rails/engine.rb:514:in `call'
railties (4.1.5) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/AviFoxi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/AviFoxi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/AviFoxi/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

根据要求 - 我的config / routes.rb

Rails.application.routes.draw do

  devise_for :admins, controllers: { registrations: "admins/registrations" }
  devise_for :users


  root to: "users#index"

  resources :users
  resources :admins
  resources :ride_years
end

1 个答案:

答案 0 :(得分:0)

尝试更改

devise_for:admins,controllers:{registrations:&#34; admins / registrations&#34; }

devise_for:admins,controllers:{registrations:&#34; admins_registrations&#34; }