0

Hartlの第8章で、ある種の重大なエラーが発生しましたが、どのように問題が発生したのかわかりません。8.2.2の直前にテストに参加しました。の実行

$ bundle exec rspec spec/models/user_spec.rb 

テストリターン

sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/models/user_spec.rb
/Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require': /Users/Lagaspi/rails_projects/sample_app/app/models/user.rb:33: syntax error, unexpected keyword_end, expecting $end (SyntaxError)
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in `require_or_load'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in `load_missing_constant'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `each'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `const_missing'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:229:in `block in constantize'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in `each'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in `constantize'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:259:in `safe_constantize'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/core_ext/string/inflections.rb:66:in `safe_constantize'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/actionpack-3.2.3/lib/action_controller/metal/params_wrapper.rb:152:in `_default_wrap_model'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/actionpack-3.2.3/lib/action_controller/metal/params_wrapper.rb:169:in `_set_wrapper_defaults'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/actionpack-3.2.3/lib/action_controller/metal/params_wrapper.rb:133:in `inherited'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/actionpack-3.2.3/lib/abstract_controller/railties/routes_helpers.rb:7:in `block (2 levels) in with'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/actionpack-3.2.3/lib/action_controller/railties/paths.rb:7:in `block (2 levels) in with'
from /Users/Lagaspi/rails_projects/sample_app/app/controllers/users_controller.rb:1:in `<top (required)>'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in `require_or_load'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:313:in `depend_on'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:225:in `require_dependency'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/engine.rb:439:in `block (2 levels) in eager_load!'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/engine.rb:438:in `each'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/engine.rb:438:in `block in eager_load!'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/engine.rb:436:in `each'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/engine.rb:436:in `eager_load!'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/application/finisher.rb:53:in `block in <module:Finisher>'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `run'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `each'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/application.rb:136:in `initialize!'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/Lagaspi/rails_projects/sample_app/config/environment.rb:5:in `<top (required)>'
from /Users/Lagaspi/rails_projects/sample_app/spec/spec_helper.rb:3:in `require'
from /Users/Lagaspi/rails_projects/sample_app/spec/spec_helper.rb:3:in `<top (required)>'
from /Users/Lagaspi/rails_projects/sample_app/spec/models/user_spec.rb:12:in `require'
from /Users/Lagaspi/rails_projects/sample_app/spec/models/user_spec.rb:12:in `<top (required)>'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
from /Users/Lagaspi/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'

Railsサーバーを実行し、ブラウザーで

http://localhost:3000/signup 

それは戻ります:

SyntaxError in UsersController#new

/Users/Lagaspi/rails_projects/sample_app/app/models/user.rb:33: syntax error, unexpected     keyword_end, expecting $end
Rails.root: /Users/Lagaspi/rails_projects/sample_app

私のブラウザがポイントされたとき

http://localhost:3000/home

それは戻ります:

Routing Error

No route matches [GET] "/home"
Try running rake routes for more information on available routes.

したがって、レーキルートを実行した後、これはターミナルに返されるものです。

sis-macbook-pro:sample_app Lagaspi$ rake routes
  users GET    /users(.:format)          users#index
        POST   /users(.:format)          users#create
new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
   user GET    /users/:id(.:format)      users#show
        PUT    /users/:id(.:format)      users#update
        DELETE /users/:id(.:format)      users#destroy
sessions POST   /sessions(.:format)       sessions#create
new_session GET    /sessions/new(.:format)   sessions#new
session DELETE /sessions/:id(.:format)   sessions#destroy
 signup        /signup(.:format)         users#new
 signin        /signin(.:format)         sessions#new
signout DELETE /signout(.:format)        sessions#destroy

そして、これが私のapp / models/user.rbファイルです

class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password

before_save { |user| user.email = email.downcase }
before_save :create_remember_token

validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence:   true,
                format:     { with: VALID_EMAIL_REGEX },
                uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end

private

def create_remember_token
  self.remember_token = SecureRandom.urlsafe_base64
end
end 

これは本当に私をジャムに陥らせました。

ルート.rb

resources :sessions, only: [:new, :create, :destroy]

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete
end

users_controller.rb

class UsersController < ApplicationController

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

  def new
@user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
   flash[:success] = "Welcome to the Sample App!"
    redirect_to @user
    else
      render 'new'
    end
  end
end

ありがとう。

4

2 に答える 2

0

また、開発プロセスで迷子になった場合(そしてそれが発生する場合)は、ここに移動して完全なコードを確認できることに注意してください。それは不正行為ではありません:-)、しかしそれはあなたを軌道に乗せ続けるでしょう。githubアカウントをお持ちの場合(必要です)、すべてを簡単にアンロードして、何が問題だったかを確認できます。

于 2012-06-06T20:19:04.620 に答える
0

ユーザーモデルには2つの目的があります。

class User < ActiveRecord::Base
    attr_accessible :name, :email, :password, :password_confirmation
    has_secure_password

    before_save { |user| user.email = email.downcase }
    before_save :create_remember_token

    validates :name, presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence:   true,
                format:     { with: VALID_EMAIL_REGEX },
                uniqueness: { case_sensitive: false }
    validates :password, presence: true, length: { minimum: 6 }
    validates :password_confirmation, presence: true
    # end - this one doesn't exist, remove it
    private

    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end
end 

これで修正されるはずです。

于 2012-06-05T16:47:29.360 に答える