rails3一个入门小程序居然就错误了求指点
本帖最后由 laohuanggua 于 2011-09-05 19:39 编辑fedora 15 i386版
ruby版本:ruby-1.8.7.352-1.fc15.i686
rails为3.0
用gem安装的。
cd /opt/learnruby
rails new mybook
而后
cd /opt/learnruby/script/
rails server可以启动。可以访问http://localhost:3000
而后添加控制器
rails generatecontroller welcome
而后修改路由器
vim config/routes.rb
在最后哪行end前面添加
get "welcome/say_hello" => "welcome#say"
编辑添加的welcome控制器
vim app/controllers/welcome_controller.rb
内容是:
class WelcomeController < ApplicationController
def say
end
end
而后添加view
vim app/views/welcome/say.html.erb
内容是<h1>Hello,World!</h1>
一切完成后,访问http://localhost:3000/mybook/welcome/say_hello
结果居然报告:
Routing Error
No route matches "/mybook/welcome/say_hello"
更苦逼的是:如果这个时候停止了rails再次启动,就会报错!!!rails都无法启动!!!
求问这个是为什么? 出错信息已经很明晰了? 本帖最后由 laohuanggua 于 2011-09-05 23:24 编辑
回复 2# 2gua
说的是找不到路由。是不是添加了controller之后才route?
错了。应该是修改了route.rb就应该有了route!我刚才访问http://localhost:3000/mybook/welcome/say同样报告:No route matches "/mybook/welcome/say" 不知道,感觉老黄瓜学RoR是不是不够系统呢?建议买本书,循序渐进看下来,这样比较好。 回复 4# 2gua
瓜瓜兄。我看的是这个
http://ihower.tw/rails3/index.html 回复 4# 2gua
瓜瓜兄。网上资料很多。这个http://www.cslog.cn/Content/ruby_on_rails_hello_world/
view部分用的是 rhtml。而不是erb文件,求问这个是不是rails2的?? 搞了一个更简单的例子。但是还是没实现。
1、生成一个controller
rails generate controller mytest
2、建立一个controller下的action
vim app/controllers/mytest_controller.rb
内容是
class MytestController < ApplicationController
def index
render_text "Hello World"
end
end
3、建立对应的view
vim app/views/mytest/index.rhtml
内容是一个随意写的字符串为fsdfdsf
访问
http://localhost:3000/mytest/index
结果提示:
Routing Error
No route matches "/mytest/index" 回复 5# laohuanggua
网络资料只能教你感知一下RoR,建议看《Rails 3 Tutorial》这本书,可以download的啊。 第一个 你路由定义了
get "welcome/say_hello" => "welcome#say"
你就应该是
http://localhost:3000/welcome/say_hello
第二个
你路由配置了么?
resources :mytest
还有一个bug
render_text 不报错么?:D 在config/route.rb 里添加一行, get "mytest/index"
还有
class MytestController < ApplicationController
def index
# render_text "Hello World"
render :text => "Hello World"
end
end
页:
[1]