ActiveRecord::StatementInvalid 在 AccountsController#new

ActiveRecord::StatementInvalid in AccountsController#new

我不确定如何解决此错误。我一按 'create account' 就收到一条错误消息 Could not find table 'accounts'.

class AccountsController < ApplicationController

   def new 
      @account = Account.new   
   end

  def create
    @account = Account.new(account_params)
    if @account.save
      redirect_to root_path, notice: 'Signed up successfully'
    else
      render action: 'new'
    end
  end 
private
  def account_params
     params.require(:account).permit(:subdomain)
  end
end

错误

ActiveRecord::StatementInvalid (Could not find table 'accounts'): 
app/controllers/accounts_controller.rb:4:in `new   

找不到 table 'accounts'

Started GET "/accounts/new" for 127.0.0.1 at 2015-01-06 13:19:40 -0500
Processing by AccountsController#new as HTML
Completed 500 Internal Server Error in 4ms

ActiveRecord::StatementInvalid (Could not find table 'accounts'):
app/controllers/accounts_controller.rb:4:in `new'


Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch 
/templates/rescues/_source.erb (10.1ms)
Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/_trace.html.erb (4.3ms)
Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/_request_and_response.html.erb (1.3ms)
Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/_web_console.html.erb (1.6ms)
Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/diagnostics.html.erb within rescues/layout (51.2ms)


Started GET "/accounts/new" for 127.0.0.1 at 2015-01-06 13:19:41 -0500
Processing by AccountsController#new as HTML
Completed 500 Internal Server Error in 1ms

ActiveRecord::StatementInvalid (Could not find table 'accounts'):
app/controllers/accounts_controller.rb:4:in `new'


Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/_source.erb (9.7ms)
Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/_trace.html.erb (4.0ms)
Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/_request_and_response.html.erb (1.3ms)
Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/_web_console.html.erb (1.4ms)
Rendered /home/shuabe/.rvm/gems/ruby-2.1.5/gems/web-console-2.0.0/lib/action_dispatch
/templates/rescues/diagnostics.html.erb within rescues/layout (38.4ms)

我不确定,但这与迁移有什么关系吗?我没有生成任何迁移。如果是迁移,如何为帐户控制器生成 table?

我目前没有使用 devise,我使用的唯一 gem 是:

gem 'factory_girl_rails', '~> 4.5.0'

gem 'rails', '4.2.0'

gem 'sqlite3'

gem 'sass-rails', '~> 5.0'

gem 'uglifier', '>= 1.3.0'

gem 'coffee-rails', '~> 4.1.0'

gem 'jquery-rails'

gem 'turbolinks'

gem 'jbuilder', '~> 2.0'

gem 'sdoc', '~> 0.4.0', group: :doc



group :development, :test do

  gem 'byebug'

  gem 'web-console', '~> 2.0'

  gem 'spring'

根据您的评论,问题是您手动创建了 Account class/model。这通常是一个非常糟糕的主意;不是致命的,但有问题,正如你所经历的那样。

通常,在 Rails 中,您将使用其中一种生成器生成模型,例如 rails g model Account [account fields]rails g scaffold Account [account fields]。这样做时,您会收到模型本身和相应的迁移(以及您需要的所有测试文件)。由于您已经指出您手动创建了模型文件,因此您有 none 个,并且需要:

  • 手动创建:

    运行 如下:

    rails g migration create_accounts [account fields]

    其中 [your account fields] 是任何必要的字段。例如,

    ... account_name:string account_status:string ...

  • 或者,暂时删除您的帐户 class,然后使用上述生成器之一重新生成它

如果您是 Rails 的新手,请查看 Getting Started guided,它说明了我在这里所指的内容。