RoR - Restful Actions
Index:
用于检索所有条目
# index.json.jbuilder json.array!(@post) do |post|
json.extract! post, :id, :title, :content
json.url post_url(post, format: :json)
end #/posts.json
Show&Destory:
1.根据id检索相应的条目
2.跳转到show.html.erb 提供相应
#example localhost:3000/posts/1 #show.json.jbuilder json.extract! @post, :id, :title, :content, :created_at, :updated_at localhost:3000/posts/2.json
respond_to: 特定怎么去相应一个request

redirect_to:

# DELETE /posts/1
# DELETE /posts/1.json def destory
@post.destory
respond_to do |format|
format.html {redirect_ to posts_url, notice: 'Post deleted' }
format.json {head :no_content }
end
end
New&Create:
# GET /posts/new def new
@post = Post.new
end # Look for new.html.erb
Create Action 没有模板,它会试着将object存进数据库,如果成功的话重定向到show 模板
如果没有成功render new action
Flash:
# flash flash[:attribute] = value # :notice (good) :alert (bad)
edit:
# GET /posts/1/edit before_action :set_post, only: [:show, :edit, :update, :destory] def edit end private
def set_post
@psot = Post.find(params[:id])
end
end
update:
用id检索到需要update的object
用edit form里面的parameters去更新object
保存更新的数据到数据库
(默认)成功的话,重定向到show 模板
不成功,到edit
before_action :set_post, only: [:show, :edit, :update, :destory] # PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json def update
respond_to do |format|
if @post.update(post_params)
format.html {redirect_to @post, notice: 'Post was updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity}
end
end
end private
def set_post
@psot = Post.find(params[:id])
end def post_params
params.require(:post).permit(:title, :content)
end
end
Partials:
# partial start with _ <%= render @posts %> is equivalent to <% @posts.each do |post| %>
<%= render post %>
<% end %>
Form Helpers and Layouts:
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<% end %> <div class="field">
<%= f.label :title %> <br>
<%= t.text_field :title %>
</div> <div class="field">
<%= f.label :content %> <br>
<%= f.text_area :content %>
</div> <div class= "actions">
<%= f.submit %>
</div>
<% end %>
如果想在controller里用model里的method,
首先defind self.method 在model里
接着用model.method 在controller里面
RoR - Restful Actions的更多相关文章
- Laravel Controllers
Basic Controllers Instead of defining all of your route-level logic in a single routes.php file, you ...
- AngularJS Resource:与 RESTful API 交互
REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格.RESTful风格的设计不仅 ...
- Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试 (转)
环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到下面的代码 LoadModule rewrite_module modules/mod_ ...
- 从英文变形规则计算到Restful Api设计
➠更多技术干货请戳:听云博客 一天在研究Restful API设计,命名的时候我总是很纠结,我相信大多数人也有这种感觉,不是说想不出来某个单词怎么写的问题,像我这种没事背单词背到13000词量的人也要 ...
- 【转】最佳Restful API 实践
原文转自:https://bourgeois.me/rest/ REST APIs are a very common topic nowaday; they are part of almost e ...
- 好RESTful API的设计原则
说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间,如有人愿意转载请注明出处,谢谢^_^ P ...
- 【转】 Build a RESTful Web service using Jersey and Apache Tomcat 2009
Build a RESTful Web service using Jersey and Apache Tomcat Yi Ming Huang with Dong Fei Wu, Qing GuoP ...
- 使用 Swagger UI 与 Swashbuckle 创建 RESTful Web API 帮助文件
作者:Sreekanth Mothukuru 2016年2月18日 本文旨在介绍如何使用常用的 Swagger 和 Swashbuckle 框架创建描述 Restful API 的交互界面,并为 AP ...
- Principles of good RESTful API Design 好的 RESTful API 设计
UPDATE: This post has been expanded upon and converted into an eBook. Good API design is hard! An AP ...
随机推荐
- 第一周——数据分析之表示 —— Numpy入门
数据的维度 从一个数据到一组数据 一个数据:表达一个含义 一组数据:表达一个或者多个含义 维度:一组数据的组织形式 一维数据 由对等关系的有序或者无序数据构成,采用线性方式组织,对应列表.数组和集合等 ...
- JVM虚拟机
一.JAVA虚拟机内存模型: 1.程序计数器:非常小的内存,用于存放下一条运行的指令: 每一个线程都必须有一个独立的程序计数器,用于记录下一条要运行的指令,是一块线程私有的内存空间,CPU时间切片 2 ...
- Mac 解决 Sourcetree 同步代码总需要密码的问题
git config --global credential.helper osxkeychain
- python学习之numpy.ewaxis
当多维数组的某一列时返回的是一个行向量 >>> X = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) >> ...
- P2866 [USACO06NOV]糟糕的一天Bad Hair Day--单调栈
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题意翻译 农夫约翰有N (N \leq 80000)N(N≤80000)头奶牛正在过乱头发节.每一头牛都站在同一排面朝东方,而且 ...
- spring boot 2.0 neo4j 使用
参考文档 官方文档 http://spring.io/projects/spring-data-neo4j#learn https://docs.spring.io/spring-data/neo4j ...
- 3.基于梯度的攻击——PGD
PGD攻击原论文地址——https://arxiv.org/pdf/1706.06083.pdf 1.PGD攻击的原理 PGD(Project Gradient Descent)攻击是一种迭代攻击,可 ...
- Hadoop| YARN| 计数器| 压缩| 调优
1. 计数器应用 2. 数据清洗(ETL) 在运行核心业务MapReduce程序之前,往往要先对数据进行清洗,清理掉不符合用户要求的数据.清理的过程往往只需要运行Mapper程序,不需要运行Reduc ...
- React实现局部刷新
[项目结构] 流程: 入口文件 -> 路由 -> layout -> Analysi/Monitor/Workspace 1.入口文件 -> src/index.js 2.组件 ...
- [转] Java程序员学C#基本语法两个小时搞定(对比学习)
Java程序员学C#基本语法两个小时搞定(对比学习) 对于学习一门新的语言,关键是学习新语言和以前掌握的语言的区别,但是也不要让以前语言的东西,固定了自己的思维模式,多看一下新的语言的编程思想. ...