2015-01-06 191 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 ,這說明了我在這裏指的是什麼。

相關問題