Rails 请教个路由的问题 示例 depot, 章节 8.1 Iteration C1: Creating the Catalog Listing

crabby · 2012年03月27日 · 最后由 Crabby 回复于 2012年03月28日 · 3494 次阅读

这本书的章节 8.1 task C1,这章节的任务是创建针对付费用户的接口

1.生成控制器 store,并且指定 action 为 index 命令如下,rails generate controller store index 这时如果访问 localhost:3000/store/index 页面展示的信息如下

Store#index Find me in app/views/store/index.html.erb

  1. 为了使展示的页面更加友好,需要设定 root url,下面是我的 route.rb 文件内容
Depot::Application.routes.draw do
get "store/index"
resources :products
# ...
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
root :to => 'store#index', :as => 'store'
# ...
end

这里面的:as=>'store'我并不清楚这个参数代表啥意思? 3.删除 public 目录中的 index.html 文件,我已经删除了 .

现在的问题是启动 server 就报错,如下信息。日志我也看的不懂,但感觉主要是 undefined local variable or method `store' ,但是 store 这个变量我需要额外定义吗?

logs:

=> Booting WEBrick
=> Rails 3.2.2 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/Crabby/depot/config/routes.rb:56:in `block in <top (required)>': undefined local variable or method `store' for #<ActionDispatch::Routing::Mapper:0x00000002a31068> (NameError)
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.2/lib/action_dispatch/routing/route_set.rb:276:in `instance_exec'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.2/lib/action_dispatch/routing/route_set.rb:276:in `eval_block'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/actionpack-3.2.2/lib/action_dispatch/routing/route_set.rb:254:in `draw'
    from /home/Crabby/depot/config/routes.rb:1:in `<top (required)>'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:245:in `load'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:245:in `block in load'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:245:in `load'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application/routes_reloader.rb:40:in `each'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/file_update_checker.rb:78:in `call'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.2/lib/active_support/file_update_checker.rb:78:in `execute'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application/finisher.rb:66:in `block in <module:Finisher>'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `run'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `each'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/application.rb:136:in `initialize!'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /home/Crabby/depot/config/environment.rb:5:in `<top (required)>'
    from /home/Crabby/depot/config.ru:4:in `require'
    from /home/Crabby/depot/config.ru:4:in `block in <main>'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
    from /home/Crabby/depot/config.ru:1:in `new'
    from /home/Crabby/depot/config.ru:1:in `<main>'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/server.rb:46:in `app'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands/server.rb:70:in `start'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:55:in `block in <top (required)>'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:50:in `tap'
    from /home/Crabby/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.2/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

:as => 'store' 会生成 store_path 这个函数,以后可以使用这个函数来转到 store#index

undefined local variable or method `store'

这个你要看看哪里没有按照书上的步骤做了,应该说用到 store 的目前就只有 route.rb,没其他的地方了。

这一章我做过了,应该没什么问题的

建议楼主先从 ruby 学起啊,不然会碰到很多本身不是莫名其妙的问题啊

楼主可以下载这本书的源码,比较一下找到错误!

#1 楼 @AlphaLiu 偶嫉妒你哈,我老碰到一些问题。我是看的 E 文第四版,理解有晦涩的地方,我在看下中文的第三版。我都没有买书,是网上下载的。 本身例子中就没有多少步骤,所以我不觉的是我哪里少做了

#2 楼 @vkill :( 是应该系统的学,可能是我 ruby 学的太草帅了吧。说是话,我没怎么看,直接上的 rails,我又借了本 programming ruby,打算细致的看下

#2 楼 @vkill 不过眼下的这个问题,挺让我蛋疼的

#3 楼 @BBQ 这个是必须的,我下载到了 rails32,对应的刚好是 3.2 版本,但是源码中的 route.rb 没有修改啥东西,有些悲剧呀

#7 楼 @Crabby 最近在学 rails 测试, 去这里 http://pragprog.com/titles/rails4/source_code 载源码包,每一章都有完整的代码

undefined local variable or methodstore' 看清楚了 'store' 不是 `store'
你的 store 前面那个引号成了 反引号。

#8 楼 @tassandar 这个我知道,源码包,我使用的是对应的 rails32 源码,有的时候的确自己懒得敲

#9 楼 @tassandar 我觉得不是这个问题,没有理由让我去敲‵ 这个键盘。我的确是使用的''

此问题,我自己已经解决,我也不知道什么时候突然好 了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号