ruby on rails 随记
2017-01-14 14:23
162 查看
learn from
codecademy
rails new myapp
bundle install
rails generate controller mycontroller
rails generate model mymodl
rake db:migrate updates the database with the new messages data model
rake db:seed seeds the database with sample data from db/seeds.rb
Ruby on Rails defines seven standard controller actions can be used to do common things such as display and modify data.
![](https://s3.amazonaws.com/codecademy-content/projects/3/seven-actions.svg)
If you want to create routes for all seven actions in your app, you can add aresource
route to your routes file. This resource route below maps URLs to the Messages controller's seven actions (
and
If you only want to create routes for specific actions, you can use
fine
tune the resource route. This line maps URLs only to the Message controller's
Open the migration file in db/migrate/ for the tags table, and add the following columns:
a
called
a
called
Next in the migration file for the destinations table, add the following columns:
a
called
a
called
a
called
the line
def show
@tag = Tag.find(params[:id])
@destinations = @tag.destinations
end
The
def update
@destination = Destination.find(params[:id])
if @destination.update_attributes(destination_params)
redirect_to(:action => 'show', :id => @destination.id)
else
render "edit"
end
end
private
def destination_params
params.require(:destination).permit(:name, :description)
end
codecademy
rails new myapp
bundle install
rails generate controller mycontroller
rails generate model mymodl
rake db:migrate updates the database with the new messages data model
rake db:seed seeds the database with sample data from db/seeds.rb
Ruby on Rails defines seven standard controller actions can be used to do common things such as display and modify data.
If you want to create routes for all seven actions in your app, you can add aresource
route to your routes file. This resource route below maps URLs to the Messages controller's seven actions (
index,
show,
new,
create,
edit,
update,
and
destroy):
resources :messages
If you only want to create routes for specific actions, you can use
:onlyto
fine
tune the resource route. This line maps URLs only to the Message controller's
indexand
showactions.
resources :messages, only: [:index, :show]
has_many :destinationsdenotes that a single Tag can have multiple Destinations.
belongs_to :tagdenotes that each Destination belongs to a single Tag.
Open the migration file in db/migrate/ for the tags table, and add the following columns:
a
stringcolumn
called
title
a
stringcolumn
called
image
Next in the migration file for the destinations table, add the following columns:
a
stringcolumn
called
name
a
stringcolumn
called
image
a
stringcolumn
called
description
the line
t.references :tag
def show
@tag = Tag.find(params[:id])
@destinations = @tag.destinations
end
The
@destinations = @tag.destinationsretrieves all the destinations that belong to the tag, and stores them in
@destinations
def update
@destination = Destination.find(params[:id])
if @destination.update_attributes(destination_params)
redirect_to(:action => 'show', :id => @destination.id)
else
render "edit"
end
end
private
def destination_params
params.require(:destination).permit(:name, :description)
end
相关文章推荐
- 采用UTF-8解决Ruby on Rails程序的中问题
- Ruby on Rails Tutorial 学习笔记 --第二章 演示程序
- 在WIN7平台下搭建Ruby On Rails web开发环境
- [ 接近ruby on rails]关于开发ROR project的准备工作
- Ruby on Rails中Rack中间件的基础学习教程
- [ruby on rails] 跟我学之(4)路由映射
- Ruby on Rails自动事务过滤器及相应的错误处理
- CentOS 下装 Ruby On Rails开发环境
- Ruby on Rails (ROR)类书籍下载地址及其他(整理)
- ruby on Rails 从后台生成二维码并保存到本地,然后返回url
- connect to Oracle in Ruby on Rails.
- linux 安装 ruby on rails
- 利用Radrails开发Ruby on Rails程序入门指南
- Ruby On Rails——创建一个基本的ROR应用程序
- 一系列学习ruby on rails的网站
- Ruby on Rails(二) - 初探Ruby on Rails, 搭建一个博客项目并发布(草稿)
- ruby on rails 【语法大全2】【模型(Models)】数据库迁移(Migrations)
- rvm,ruby on rails环境完美配置教程(下)
- Ruby on Rails,服务端如何响应页面提交的请求
- linux Ruby on Rails安装运行第一个小程序的两 个问题