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. 【原创】大叔经验分享(24)hive metastore的几种部署方式

    hive及其他组件(比如spark.impala等)都会依赖hive metastore,依赖的配置文件位于hive-site.xml hive metastore重要配置 hive.metastor ...

  2. 还在期待安卓9.0吗?Android 10.0要来了

    目前,美国 Google公司的 AndroidP (安卓9.0),已经正式全面推出有几个多月了.众多手机品牌厂商也都在积极的进行更新适配 Android 9.0 系统(修改UI界面也算是二次开发,嗯) ...

  3. Javascript我学之四作用域

    本文是金旭亮老师网易云课堂的课程笔记,记录下来,以供备忘 作用域 JavaScript的变量作用域只有两种,全局作用域和函数作用域. 函数的作用域 函数中定义的变量是私有的,仅在本函数范围内有效,称为 ...

  4. js分析 邮箱地址加密 [email protected]

    0.参考 https://segmentfault.com/q/1010000000117476 javascript里function之前加上感叹号 ' ! ' 会怎么样? // 这么写会报错,因为 ...

  5. pyqt pyside 设置窗口关闭时删除自身

    pyqt pyside 设置窗口关闭时删除自身 self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

  6. elk安装时最常见的报错

    1.在启动kibana的时候报一下错误 max file descriptors [4096] for elasticsearch process likely too low, increase t ...

  7. 初学Python的一些细节

    一.python的数据类型 1.python的基本数据类型包括数值数据类型和字符串数据类型:基本数据类型的特点是不允许改变,如果改变基本数据类型的值,会导致内存的重新分配. int 整形 二进制    ...

  8. sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError)

    在我学习flask建立网站时间碰到了一个棘手的问题,就是在我进行操作日志的更新时间,发现表格建立有点错误,导致表缺失,从而报了下面的错误 sqlalchemy.exc.ProgrammingError ...

  9. vscode断点调试简单的服务端文件

    一.无配置文件时,如何调试? 1.安装code命令打开vscode,快捷键command + shift + p,输入shell command,选择install 'code' command in ...

  10. 我的 FPGA 学习历程(04)—— 练习 verilog 硬件描述语言

    这篇讲的是使用 verilog 硬件描述语言编写一个 3 - 8 译码器. 3 - 8 译码器是一个简单的组合逻辑,用于实现并转串,其输入输出关系如下: | 输入  |  输出  | -------- ...