ror突进-p3

新建一个rails项目

localhost:testpro alan$ mkdir railsbridge
localhost:testpro alan$ cd railsbridge/
localhost:railsbridge alan$ rails new suggestotron
...
localhost:railsbridge alan$ ls
suggestotron
localhost:railsbridge alan$ cd suggestotron/
localhost:suggestotron alan$ ls
Gemfile      Rakefile     config       lib          test
Gemfile.lock app          config.ru    log          tmp
README.md    bin          db           public       vendor
localhost:suggestotron alan$ subl .

原来有subl真的方便不少

app/ : 包含你应用程式的 controllers、models、views。你要改的东西大多是这些。

config/ : 设定应用程式的执行阶段规则、路由设定(routes)、资料库等等。

db/ :显示你目前资料库的 schema(结构定义),以及资料库的 migrations。

public/ :这是唯一一个资料夹会是放什么就出现什么的。如果你把档案放里面,server 会直接回传,不会经过 Rails 的处理。

app/assets/ :你会要把图片、JavaScript、stylesheets (CSS) 还有其他静态档案放在里面。现代的 Rails 应用程式使用一种叫做 Assets Pipeline 的东西,把在这资料夹里面的所有 JavaScript 和 CSS 档合并成一个档案来加速。

启动程序

rails server

原来rails的s是server的意思

建立数据表格

migration好像是迁移的意思,暂时不知道为什么这么叫

localhost:suggestotron alan$ rails generate scaffold topic title:string description:text

原来这是一个建立数据结构的命令

generate scaffold 告诉 Rails 去建立一堆档案让 topics 可以动。
topic 是告诉 rails 新的 Model 的名字是什么。
title:string 是说 topics 有标题(title),它是一个字串(String)。
description:text 是说 topics 有内文(description),它是一段文字(Text)。(“字串”跟“文字”的差别?基本上“文字”是用来储存可能会很长的字串。)

更新资料库存储Model表格

localhost:suggestotron alan$ rake db:migrate
== 20160924152010 CreateTopics: migrating =====================================
-- create_table(:topics)
   -> 0.0012s
== 20160924152010 CreateTopics: migrated (0.0013s) ============================

MVC架构

虽然一直用声称是MVC的框架,但依旧没有很好的掌握到底怎么才算MVC了这张图片倒是很明了

访问数据

当访问 http://localhost:3000/topics 时候,注意到了后台执行了一个sql查寻

Started GET "/topics" for 127.0.0.1 at 2016-09-24 23:34:06 +0800
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by TopicsController#index as HTML
  Rendering topics/index.html.erb within layouts/application
  Topic Load (0.2ms)  SELECT "topics".* FROM "topics"
  Rendered topics/index.html.erb within layouts/application (13.3ms)
Completed 200 OK in 887ms (Views: 871.8ms | ActiveRecord: 0.4ms)

当我插入一条数据时,发现后台果然使用post更新了数据

Started POST "/topics" for 127.0.0.1 at 2016-09-24 23:37:22 +0800
Processing by TopicsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"DyMpqlDyrpyrtQWf5y1/Dc6NpdOc4fNsogZXMd2Q7Qxjf3noQb1lDdcPLk1MFIEYOuuEbMVzanR0I7/PwmZOSQ==", "topic"=>{"title"=>"First topic", "description"=>"First topic description"}, "commit"=>"Create Topic"}
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "topics" ("title", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["title", "First topic"], ["description", "First topic description"], ["created_at", 2016-09-24 15:37:22 UTC], ["updated_at", 2016-09-24 15:37:22 UTC]]
   (1.3ms)  commit transaction
Redirected to http://0.0.0.0:3000/topics/1
Completed 302 Found in 10ms (ActiveRecord: 2.0ms)


Started GET "/topics/1" for 127.0.0.1 at 2016-09-24 23:37:22 +0800
Processing by TopicsController#show as HTML
  Parameters: {"id"=>"1"}
  Topic Load (0.3ms)  SELECT  "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Rendering topics/show.html.erb within layouts/application
  Rendered topics/show.html.erb within layouts/application (0.9ms)
Completed 200 OK in 37ms (Views: 28.6ms | ActiveRecord: 0.3ms)