JamesRocky
JamesRocky

Reputation: 721

ActiveRecord::StatementInvalid in AccountsController#new

I'm not sure what to do to fix this error. As soon as I press the 'create account' I get an error saying 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

error

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

Could not find 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)

I'm not sure, but does it have anything to do with migration? I haven't generated any migration. If it is migration, how do I generate a table for the accounts controller?

I'm not currently using devise and the only gems I'm using are:

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'

Upvotes: 0

Views: 500

Answers (1)

Paul Richter
Paul Richter

Reputation: 11072

Based on your comment, the problem is you've created the Account class/model manually. This is generally a very bad idea; not fatal, but problematic, as you've experienced.

Normally, in Rails you would generate your models using one of the generators, such as rails g model Account [account fields], or rails g scaffold Account [account fields]. When doing so, you receive the model itself and the corresponding migration (as well as all the testing files you need). Since you've indicated you created the model file manually, you have none of that, and need to either:

  • Create it manually:

    run the following:

    rails g migration create_accounts [account fields]

    where [your account fields] would be whatever fields are necessary. For example,

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

  • OR, remove your Account class for now, and regenerate it using one of the generators above

If you're new to Rails, check out the Getting Started guided, which illustrates what I'm referring to here.

Upvotes: 2

Related Questions