首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Rails3课程系列之五:Rails3入门(5)

2012-12-18 
Rails3教程系列之五:Rails3入门(5)文章出处:http://edgeguides.rubyonrails.org/getting_started.html?1.

Rails3教程系列之五:Rails3入门(5)

文章出处:http://edgeguides.rubyonrails.org/getting_started.html

?

1. 添加第二个模型

在前面的教程中,我们已经学会的使用脚手架快速搭建一个简单的应用,毕竟脚手架不能做任何事情,现在我们需要在应用中添加第二个模型了。

?

模型在rails中使用单数形式,而其相关的数据库将使用复数名称。

?

那么对于一个博客来说,评论总是少不了的,我们现在就要创建一个 Comment 模型。对于大多数的 rails 程序员来说,一般都是通过rails的生成器来生成模型,在这里我们也一样:

?

$ rails g model comment commenter:string body:text post:references

?

该命令将创建4个文件:

?

  • app/models/comment.rb?– 模型
  • db/migrate/20101128142329_create_comments.rb?– 迁移文件
  • test/unit/comment_test.rb?and?test/fixtures/comments.yml?– 测试文件.首先,我们看一下 comment.rb
       get "hello/say_hello" 
    # get "hello/say_time"
      # get "hello/say_greeting"
       match ':controller(/:action(/:id(.:format)))'
    end
    这里很乱啊
    标准的ruby block写法

    试试
    resources :books do
      resources :comments
    end
    resources :users
    后面的注释掉 
    老师好帮我看个问题行吗。
    ~接触ruby跟rails 半个月了,我这按照ror的入门在学习,我这里遇到了问题。
    提示我 【comments】没有,我的代码对过2遍了,没错。帮我看看吧,谢谢了!
    undefined method `comments' for nil:NilClassRails.root: D:/Rubydemo/demo3

    Application Trace | Framework Trace | Full Trace
    app/controllers/comments_controller.rb:5:in `create'
    Request
    Parameters:

    {"utf8"=>"?",
    "authenticity_token"=>"8IMu5tG9HDusyxAf3n1WCKYUtmRvH16CEDqt93n9+sc=",
    "comment"=>{"commenter"=>"阿",
    "body"=>"猜猜"},
    "commit"=>"Create Comment",
    "post_id"=>"7"}

热点排行