2011-08-31 31 views
0

我對Rails比較陌生,每當我測試登錄頁面時,我都會收到上述錯誤。 下面是測試視圖文件:爲什麼我會收到「無法找到表」用戶的錯誤?

<%= form_for :user, :url => create_path do |f| %> 
    <p> Email: <br /> <%= f.text_field :email %></p> 
    <p> Password: <br /> <%= f.password_field :password %></p> 
    <p><%= submit_tag "Sign In", :disable_with => "Please wait...", :class => "btn primary" %></p> 
<% end %> 

這裏的用戶控制器:

class UsersController < ApplicationController 
    before_filter :get_user, :except => [:register, :login, :create] 
    before_filter :user_signed_in?, :only => [:delete] 

    def create 
    if user = User.find_by_email(params[:user][:email]).try(:authenticate, params[:user][:password]) 
     session[:user_id] = user.id 
     redirect_to root_path # Or whatever you want i.e. redirect_to user 
    else 
     render :new, :flash => { :error => "Bad email/password combination" } 
    end 
    end 

    def delete 
    session.delete(:user_id) 
    end 

    def register 
    if Invite.find_by_hash(params[:hash]).blank? 
     redirect_to root_path 
     return 
    end 
    @user = User.new(params[:user]) 
    if(request.post? and @user.save) 
     flash[:notice] = "Account Created Successfully" 
     redirect_to root_path  
     return 
    else 
     flash.now[:warning] = @user.errors.full_messages 
    end 
    end 

    def destroy 
    @user = User.find(params[:id]) 
    @user.destroy 
    redirect_to root_path 
    end 

    def login 

    end 

    def get_user 
    @user = User.find(params[:id]) 
    end 

end 

我的用戶模型:

class User < ActiveRecord::Base 
    has_secure_password 
    validates :email, :presence => true, :uniqueness => true, :length => {:minimum => 6} 
    validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i 
    validates :name, :presence => true, :length => {:maximum => 32} 
    validates :password, :presence => true, :length => {:minimum => 8} 
    validates :username, :presence => true, :length => {:maximum => 20}, :uniqueness => true 
    validates :blog, :uniqueness => true 
    has_many :posts 
end 

我得到確切的錯誤是這樣的:

ActiveRecord::StatementInvalid in UsersController#create 
Could not find table 'users' 

P.S .:我正在運行Rails 3.1.0.rc8。任何幫助,將不勝感激。謝謝。

更新:我似乎無法找到任何表。我剛剛升級到Rails 3.1,在此之前一切都很好。

+0

你在應用程序根目錄上運行rake db:migrate命令嗎? – rajkamal

+0

是的,我有,但它沒有奏效。 – Sam

回答

1

如果您剛剛升級到Rails 3.1並且升級前一切正常,請檢查您的database.yml配置。我懷疑配置是否由於升級而回到了默認值。

如果沒有,請遵循Rajkamal的建議並確保您運行遷移。

+0

我剛剛檢查了我的database.yml文件,一切都和以前一樣。難道是我使用mysql而不是mysql2? – Sam

+0

是的..檢查你的Gemfile,如果你已經安裝了mysql2 gem,那麼適配器應該是mysql2 – dexter

+0

我剛剛把Gemfile中的mysql更改爲mysql 2以及database.yml文件,當運行「bundle install」時,在安裝mysql2時出錯:'用本機擴展安裝mysql2(0.3.7)C:/Rubar192/lib/ruby/site_ruby/.9.1/rubygems/installer.rb:552:in build in block in build_extensions:ERROR : 無法構建gem原生擴展。 (Gem :: Installer :: ExtensionBuildError)'我永遠無法在Windows上安裝mysql2。也許你可以幫忙? – Sam

相關問題