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的更多相关文章

  1. Laravel Controllers

    Basic Controllers Instead of defining all of your route-level logic in a single routes.php file, you ...

  2. AngularJS Resource:与 RESTful API 交互

    REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格.RESTful风格的设计不仅 ...

  3. Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试 (转)

    环境配置: 开启服务器伪静态 本处以apache为例,查看apache的conf目录下httpd.conf,找到下面的代码 LoadModule rewrite_module modules/mod_ ...

  4. 从英文变形规则计算到Restful Api设计

    ➠更多技术干货请戳:听云博客 一天在研究Restful API设计,命名的时候我总是很纠结,我相信大多数人也有这种感觉,不是说想不出来某个单词怎么写的问题,像我这种没事背单词背到13000词量的人也要 ...

  5. 【转】最佳Restful API 实践

    原文转自:https://bourgeois.me/rest/ REST APIs are a very common topic nowaday; they are part of almost e ...

  6. 好RESTful API的设计原则

    说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间,如有人愿意转载请注明出处,谢谢^_^ P ...

  7. 【转】 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 ...

  8. 使用 Swagger UI 与 Swashbuckle 创建 RESTful Web API 帮助文件

    作者:Sreekanth Mothukuru 2016年2月18日 本文旨在介绍如何使用常用的 Swagger 和 Swashbuckle 框架创建描述 Restful API 的交互界面,并为 AP ...

  9. 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 ...

随机推荐

  1. 【算法】【python实现】二叉树深度、广度优先遍历

    二叉树的遍历,分为深度优先遍历,以及广度优先遍历. 在深度优先遍历中,具体分为如下三种: 先序遍历:先访问根节点,再遍历左子树,再遍历右子树: 中序遍历:先遍历左子树,再访问根节点,再遍历右子树: 后 ...

  2. 【原创】大叔问题定位分享(25)ambari metrics collector内置standalone hbase启动失败

    ambari metrics collector内置hbase目录位于 /usr/lib/ams-hbase 配置位于 /etc/ams-hbase/conf 通过ruby启动 /usr/lib/am ...

  3. C# 最牛逼的Utility工具类

    完整代码: using System; using System.Collections.Specialized; using System.IO; using System.Net; using S ...

  4. java第一个demo(简单登陆窗体)

    首先新建一个Maven项目 选择一个存放项目的目录 ,点击完成(下图). 为了防止jdk版本的问题,所以在pom.xml里面做一个配置,让整个项目统一用jdk 1.8版本(1.7之前可能会存在一些问题 ...

  5. Java_日期时间相关类

    目录 Date类(java.util.date) Calendar类(java.util.Calendar) SimpleDateFormat类(java.text.SimpleDateFormat) ...

  6. 2018秋季C语言学习总结

    2018秋季开始学习c语言 1.printf格式化输出函数 2.基本数据类型,int整型,float浮点型,double双精度浮点型,char字符型 3.算数运算符 +加法,-减法,*乘法,/除法,% ...

  7. thinkPHP中M()和D()的区别

    在实例化的过程中,经常使用D方法和M方法,这两个方法的区别在于M方法实例化模型无需用户为每个数据表定义模型类,如果D方法没有找到定义的模型类,则会自动调用M方法.通俗一点说:M实例化参数是数据库的表名 ...

  8. SignalR 实时推送消息

    业务场景 以前做过一个东西,就是当数据库有数据更新的时候,能够自动更新到前台,那时候signalr还没出现的时候,需要自己实现轮询读库,对于数据库和程序都是比较郁闷的事情.现在利用SignalR解决数 ...

  9. BZOJ3257 [Zjoi2014]力 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8762639.html 题目传送门 - BZOJ3527 题意 给出长度为$m$的序列$q_{1..m}$,让你输 ...

  10. LCA最近公共祖先

    不会 准备研究一波!!! #include<bits/stdc++.h> ; using namespace std; vector<int> g[maxn]; ][maxn] ...