Hartl第8章的Ruby on Rails教程

时间:2012-06-05 16:17:48

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 railstutorial.org

我通过Chapter 8 of Hartl确实犯了一些严重的错误,但我不确定它是怎么回事。我在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.

因此,在运行rake路由后,这是终端返回的内容:

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 

这个让我陷入了困境。

的routes.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

感谢。

2 个答案:

答案 0 :(得分:0)

您的用户模型有两个目的:

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 

这应该解决它。

答案 1 :(得分:0)

另外,请注意,如果您在开发过程中迷路(并且会发生),请注意您可以转到here并查看完整代码。这不是作弊:-),但它会让你保持在轨道上。如果您有一个github帐户(您应该),您可以轻松地将其全部重新加载并检查出错的地方。