- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
提前为这个问题的冗长道歉。如果您耐心等待我,我想您会发现它实际上非常简单……鉴于我有限的 Rails 领域知识,我很难解释清楚。
鉴于 an ActionController commit dated Aug 6 中的这条评论:
=== Builtin HTTP verb semantics
Rails default renderer holds semantics for each HTTP verb. Depending on the
content type, verb and the resource status, it will behave differently.
Using Rails default renderer, a POST request for creating an object could
be written as:
def create
@user = User.new(params[:user])
flash[:notice] = 'User was successfully created.' if @user.save
respond_with(@user)
end
Which is exactly the same as:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to(@user) }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
The same happens for PUT and DELETE requests.
我修改了一个非常基本的 Controller 以使用respond_with
。当 Rails 自动生成的测试尝试将空的 params
传递给更新和创建方法时,除了 2 个规范失败外,一切似乎都运行良好。我可以用一个简单的 if save/else 来纠正这种行为,但我试图理解这个"new"功能。我认为默认规范可能是以过时的方式编写的。
来自提交评论:“由于请求是 POST,respond_with 将检查 @people 资源是否有错误。如果有错误,它将呈现具有不可处理实体状态 (422) 的错误对象。
”
因此向下滚动到 POST(下方)下的最后一个测试/规范,我是否可以重写它以测试“不可处理的实体状态 (422)”并通过,因此一切都很好?
我的 Controller :
class ClownsController < ApplicationController
respond_to :html, :json
def index
respond_with(@clowns = Clown.all)
end
def show
respond_with(@clown = Clown.find(params[:id]))
end
def new
respond_with(@clown = Clown.new)
end
def edit
respond_with(@clown = Clown.find(params[:id]))
end
def create
@clown = Clown.new(params[:clown])
flash[:notice] = 'Clown was successfully created.' if @clown.save
respond_with(@clown)
end
# Replacing def create above with this won't Fail the spec ##
#
# def create
# @clown = Clown.new(params[:clown])
# respond_with(@clown) do |format|
# if @clown.save
# flash[:notice] = 'Clown was successfully created.'
# format.html { redirect_to @clown }
# else
# format.html { render :action => :new }
# end
# end
# end
def update
@clown = Clown.find(params[:id])
flash[:notice] = 'Clown has been updated.' if @clown.update_attributes(params[:clown])
respond_with(@clown)
end
def destroy
@clown = Clown.find(params[:id])
flash[:notice] = 'Successfully deleted clown.' if @clown.destroy
respond_with(@clown)
end
end
测试规范:
$ rspec spec/
.......F....F..............
Failures:
1) ClownsController POST create with invalid params re-renders the 'new' template
Failure/Error: response.should render_template("new")
expecting <"new"> but rendering with <"">.
Expected block to return true value.
# (eval):2:in `assert_block'
# ./spec/controllers/clowns_controller_spec.rb:69:in `block (4 levels) in <top (required)>'
2) ClownsController PUT update with invalid params re-renders the 'edit' template
Failure/Error: response.should render_template("edit")
expecting <"edit"> but rendering with <"">.
Expected block to return true value.
# (eval):2:in `assert_block'
# ./spec/controllers/clowns_controller_spec.rb:107:in `block (4 levels) in <top (required)>'
这是 clowns_controller_spec.rb 的一部分:
require 'spec_helper'
describe ClownsController do
def mock_clown(stubs={})
(@mock_clown ||= mock_model(Clown).as_null_object).tap do |clown|
clown.stub(stubs) unless stubs.empty?
end
end
...
describe "POST create" do
describe "with invalid params" do
it "re-renders the 'new' template" do
Clown.stub(:new) { mock_clown(:save => false) }
post :create, :clown => {}
response.should render_template("new")
end
end
最佳答案
尝试用以下代码模拟你的 Clown 类
Clown.stub(:new) { mock_clown(:errors => {:any => 'error'}) }
这样 respond_with 方法就会知道模型保存失败并渲染新模板。
关于ruby-on-rails - 使用和测试 Rails ActionController 的 respond_with,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4309300/
来自 rails 4.2 , respond_with和非实例级别 respond_to已移至responders gem 。如果我想写一个 json 响应所以我不需要 responders gem
我正在阅读 Rails 3 in Action 中的第 13 章,我在 respond_with 方法中使用哈希 :methods => "last_ticket"。但是我不明白这是做什么的,也不知道
我正在 Rails 3.1 中构建一个简单的 json API。我创建了一个具有两个功能的 Controller : class Api::DogsController "success"})
我有一个在 ruby v.1.9.3 上运行的 Rails 3 应用程序,具有用户、帖子和角色模型。一个用户拥有多个帖子和角色。 我的routes.rb 文件如下所示: namespace :ap
我有一个在 ruby v.1.9.3 上运行的 Rails 3 应用程序,带有用户、帖子 和角色 模型。一个用户有很多帖子和角色。 我的 routes.rb 文件是这样的: namespace :
尝试在 Rails Controller 中使用返回时遇到问题。这不起作用: class UsersController ): app/controllers/users_controller.rb:
我正在尝试使用 respond_with 方法使用 rails 制作一个简单的 rest api,但它不想输出任何类型的状态消息 例如,我希望它不仅返回 get 调用的 json,还返回 ok 状态。
我试图在 respond_to JSON 散列中包含一个虚拟属性/方法。 模型 (employee.rb) attr_reader :my_method def my_method return
我有一个简单的模型: class Receipt include ActiveModel::Serialization attr_accessor :products end 我的 Contr
我正在尝试在 Rails 中提供自定义 404 响应,但我不确定如何将 :status 赋予 respond_with 函数。 我知道使用 render,我可以这样做: render :status
我的一个 Controller 中有以下内容: def create photo.user = current_user if current_user flash[:notice] = "T
在我的项目中我有 respond_to :json load_and_authorize_resource def show respond_with @job_pattern end 按照这里的
我正在寻找一种方法来缩短生成 json 的 respond_with 中的 :include => :child。 这是一个例子,不确定是否可行,但我想知道。 在 Controller 中: @p =
在为提供 json 的 Rails 3.0.3 应用构建 API 时,发生了一些意外行为。 以下是 Controller 。问题是关于 respond_with。我已经在应用程序 Controller
生成一些 json 时遇到问题。我正在尝试渲染像这样的 json 的单个事件记录结果: @data = User.find(1) respond_with(@data, :include => :st
在 Controller 中,我想替换 if..render..else..render与 respond_with : # Current implementation (unwanted) def
respond_to有什么区别和 respond_with ? 他们在做什么? 任何人都可以发布带有输出屏幕截图的示例吗? 谢谢。 最佳答案 有一个非常完整的答案here .本质上 response_
为什么在这种情况下respond_with不响应json?我使用显式 .json (/tasks/4e3c1163a19d461203000106/items/4e4c27dfa19d46e0e400
提前为这个问题的冗长道歉。如果您耐心等待我,我想您会发现它实际上非常简单……鉴于我有限的 Rails 领域知识,我很难解释清楚。 鉴于 an ActionController commit dated
我创建了一个用 json 响应的简单 api。当我尝试从浏览器调用它时,我得到了适当的 json 响应,但是当我尝试从 actionscript 远程调用它时,它似乎试图调用 json,就好像它是一个
我是一名优秀的程序员,十分优秀!