rails 笔记

按惯例先上网址:  http://guides.ruby-china.org/    适合初学者很好的文章

 ===========================rails 命令=====================================

1.创建controler: welcome ;action: index

$ rails generate controller welcome index

2.创建名为Article的model定义title:string text:text
$ bin/rails generate model Article title:string text:text 
$ bin/rake db:migrate 迁移数据

3.创建model 添加关联
$ rails generate model Comment commenter:string body:text article:references

4.在文件中 config/routes.rb 添加一个rest 动作
Rails.application.routes.draw do
resources :articles
end

$ bin/rake routes

5.进入控制台
rails c

 

 

===========================知识点=====================================

1.html 选择时间的input标签   Date.today.to_s

  <input type="date" value="<%=Date.today.to_s%>" id="begin" style="width:100%;">

2. action 返回json 数据 

  render json: res.to_json

3. 返回到article 数据到view

  redirect_to article_path(@article)

4.  permit 方法允许在动作中使用 title 和 text 属性

  def create

@article = Article.new(article_params)
@article.save
redirect_to @article

end

private

def article_params
params.require(:article).permit(:title, :text)

end

 

你可能感兴趣的:(rails 笔记)