11月29日 The Rails philosophy 完成rails on guide 的第一章getting started with rails

时间:2023-03-09 06:37:25
11月29日 The Rails philosophy 完成rails on guide 的第一章getting started with rails

the rails philosophy includes two major guiding principles:

Don't repeat yourself: DRY is a principle of software development which states that "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system". By not writing the same information over and over again. our code is more maintainable,   more extensible, and less buggy.

Convention over Configuration: Rails has opinions about the best way to do many things in a web application, and defaults to this set of conventions, rather than require that you specify every minutiae through endless configuration files.



通过rubyonrails-guides,建立一个博客(全英文阅读)

命令:

rails new -h //看帮助help.

link_to  //method,Rail's built-in view helpers.

**

<%= link_to("My Blog", controller: "articles")%>  //当你链接到一个行为,而不在同一个controller的时候,你需要指定:controller option.

注释:⚠️ guide里有关于调用error_explanation的使用方法。如:pluralize(), @name,errors.full_messages.


❌:

1.undefined method `each' for nil:NilClass

分析:问题出在nilclass,each指向的class是空的。看一下传入的实例变量名字是不是写错了。


单词:

distinction:(区别)

trm/ (temporary:) 临时文件,缓存和pid

execute(kill sb/do sth/ferform an action)

malicious [mə'lɪʃəs]  (very unkind  and cruel, and deliberately behaving in a way that is likely to upset or hurt someone )

proceed:(verb, continue to do one thing)

process:(n,过程,进程,v,加工处理食品,数据等)

retrieve [rɪ'triːv]: to find sth and bring it back formal. to get back information that has been stored in the memory of a computer technical.检索。


new.html.erb 文件中。文件的扩展名很重要:第一个扩展名是模板格式,第二个扩展名是模板处理器。这个模板的格式只能是 html,模板处理器只能是 erbbuilder和 coffee 中的一个。:erb 是最常用的 HTML 模板处理器,:builder 是 XML 模板处理器,:coffee 模板处理器用 CoffeeScript 创建 JavaScript 模板。因为我们要创建 HTML 表单,所以应该使用能够在 HTML 中嵌入 Ruby 的 ERB 语言。

5.6 saving data in the controller

Class names in Ruby must begin with a capital letter.

render plain: params[:article].inspect 返回参数key的值并显示在屏幕上。

5.10 Adding Some Validation

redirect_to 和 render的一个区别

redirect_to :tell the browser to issue another request.(因此效能较低)

render: is done within the same request as the form submission(submit提交)

5.11 Updating Articles

@article.update(article_params) 更新全部,也可以更新部分内容,如下:

@article.update(text:"A new title")

formhelper activeview::helpers

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style

t.integer :supplier_id 更明确地表明了外键的名称。在目前的 Rails 版本中,可以抽象实现的细节,使用 t.references :supplier 代替。

foreign_key 用于定义给关联的索引换一个名字

@comment = @article.comments.create  //create相当于new and save.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Arial; color: #3d3d3d; -webkit-text-stroke: #3d3d3d}
span.s1 {font-kerning: none}

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; color: #666666; -webkit-text-stroke: #666666}
span.s1 {font-kerning: none}

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Arial; color: #646464; -webkit-text-stroke: #646464}
span.s1 {font-kerning: none}