ruby on rails3 study notes
来源:互联网 发布:windows自带画图软件 编辑:程序博客网 时间:2024/06/16 11:56
use rails 3.2.5 version
build project:
rails new {project_name} -d mysql
running rails server:
script/rails server -p 80
the first form:
add route in config/routes.rb
root :to => "posts#new" //set the home page
get "posts/new" //a new route that only responds to GET requests,
--------------------------------------------------------------------------------------------------------
script/rails generate controller post new//"new" is the method name
vi app/views/posts/new/html.erb //add the following code
<%= form_for :post, :url => { :action => :create } do |f| %>
//:url => { :action => :create } set post action
<p>
<%= f.label :title %> <br>
<%= f.text_field :title %>
</p>
<p> <%= f.label :text %> <br>
<%= f.text_area :text %>
</p>
<p> <%= f.submit %> </p>
<% end %>
--------------------------------------------------------------------------------------------------------
vi config/routes.rb //add following post route
post "posts/create"
--------------------------------------------------------------------------------------------------------
vi app/controllers/posts_controller.rb //add method create
--------------------------------------------------------------------------------------------------------
open app/view/posts/show.html.erb and add the following content:
5.8 Listing all posts
http://edgeguides.rubyonrails.org/getting_started.html
to be continued......
build project:
rails new {project_name} -d mysql
running rails server:
script/rails server -p 80
the first form:
add route in config/routes.rb
root :to => "posts#new" //set the home page
get "posts/new" //a new route that only responds to GET requests,
--------------------------------------------------------------------------------------------------------
script/rails generate controller post new//"new" is the method name
vi app/views/posts/new/html.erb //add the following code
<%= form_for :post, :url => { :action => :create } do |f| %>
//:url => { :action => :create } set post action
<p>
<%= f.label :title %> <br>
<%= f.text_field :title %>
</p>
<p> <%= f.label :text %> <br>
<%= f.text_area :text %>
</p>
<p> <%= f.submit %> </p>
<% end %>
--------------------------------------------------------------------------------------------------------
vi config/routes.rb //add following post route
post "posts/create"
--------------------------------------------------------------------------------------------------------
vi app/controllers/posts_controller.rb //add method create
class
PostsController < ApplicationController
def
new
end
def
create
#render :text => params[:post].inspect
//The render method here is taking a very simple hash with a key of text and value of params[:post].inspect
@post = Post.new(params[:post])
@post.save
redirect_to :action => :show, :id => @post.id
//save data to database
end
def
show
@post
= Post.find(params[
:id
])
end
end
--------------------------------------------------------------------------------------------------------
rails generate model Post title:string text:text //create the post model
//this command will create a database structure file
//app/models/post.rb anddb/migrate/20120419084633_create_posts.rb
(name could be a bit different)
--------------------------------------------------------------------------------------------------------
rake db:migrate
//Rails will execute this migration commond and tell you it created the post table
//Because you’re working in the development environment by default
//If the production environment you must explicitly pass it when invoking the commond
//rake db:migrate RAILS_ENV=production.
Open config/routes.rb and add the following route:
get
"posts/:id"
=>
"posts#show"
open app/view/posts/show.html.erb and add the following content:
<
p
>
<
strong
>Title:</
strong
>
<%=
@post
.title
%>
</
p
>
<
p
>
<
strong
>Text:</
strong
>
<%=
@post
.text
%>
</
p
>
5.8 Listing all posts
http://edgeguides.rubyonrails.org/getting_started.html
to be continued......
- ruby on rails3 study notes
- ruby on rails3 常见问题总结
- Linux on Mac study notes (1)
- ruby on rails3 入门 ——尝鲜吧!骚年
- study notes
- Ruby On Rails 学习:Rails3中实现数据分页和排序
- ruby on rails 指导入门 http://ihower.tw/rails3/firststep.html
- Ruby Notes
- Ruby notes
- RUBY NOTES
- Seven languages in seven weeks (notes on Ruby)
- Ruby notes #1 Environment setup on Win32 platform
- MySql study notes [1]
- Portlet Study Notes (1)
- IBM Director study notes
- lucene study notes
- lucene study notes (2)
- Legacy Study notes(Servlet)
- 《信息检索导论》学习笔记
- Rails3 Route 用法集锦
- QT中简单的延时办法
- C#栈和堆的讲解
- 算法是内功
- ruby on rails3 study notes
- AIX下AWK语言中使用正则表达式
- 复习负数的二进制表示方法
- 水晶报表10高级开发版下载及序列号
- ORA-09925 Linux-x86_64 Error: 2: No such file or directory
- .Net 中的托管与非托管 (CLR)
- socket之select函数
- Spring mvc动态多数据源
- 网络字节序与主机字节序