2015-01-06 190 views
0

我不知道该怎么做才能解决这个错误。只要按“创建帐户”,我就会收到一条错误消息,说找不到表'帐户'。ActiveRecord :: StatementInvalid AccountsController#new

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 

找不到表 '账户'

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) 

我不知道,但它有什么关系迁移?我没有产生任何迁移。如果是迁移,我如何为账户控制器生成表格?

我不是目前使用的设计和我使用的唯一的宝石:基于您的评论

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' 
+0

检查你的数据库,你真的有一个名为'accounts'的表吗?在rails中,如果你使用其中一个生成器创建你的模型(例如'rails g model Account'),那么你就可以进行迁移。如果您手动创建模型,那么您已经为自己做了一些额外的工作,并且需要[自己创建迁移](http://stackoverflow.com/a/14452094/877472)。 –

+0

您是否创建帐户模型?你是否迁移了数据库rake db:migrate? –

+1

@RailsOuter如果他们忘记迁移,他们实际上会得到一个不同的错误(通常是说“等待迁移”)。 –

回答

2

,问题是你手动创建Account类/模型。这通常是一个非常糟糕的主意;不是致命的,而是有问题的,就像你所经历的那样。

通常,在Rails中,您可以使用其中一个生成器生成模型,例如rails g model Account [account fields]rails g scaffold Account [account fields]。这样做时,您会收到模型本身以及相应的迁移(以及您需要的所有测试文件)。既然你已经表明你手动创建模型文件,你有没有这一点,并需要:

  • 手动创建它:

    运行以下命令:

    rails g migration create_accounts [account fields]

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

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

  • OR,您的帐户类,现在,使用一台发电机以上

如果你是新来的Rails其再生,检查出Getting Started guided ,这说明了我在这里指的是什么。

相关问题