这是我能够解决的先前问题的延续: 参数未正确保存(Rails) 但现在我遇到了一个陌生人的问题。 虽然我可以获取要保存的参数,但是当我尝试引用它时它没有响应。 该模型: # == Schema Information # # Table name: messages # # id :integer not null, primary key # content :text # sender_id :integer # recipient_list :text # created_at :datetime not null # updated_at :datetime not null # class Message < ActiveRecord::Base attr_accessible :content, :sender_id, :recipient_list attr_reader :recipient_list #necessary for jquery-token-fields […]
我正在使用xhr:post与控制器交互,我期待重定向。 在js.erb中我有window.location.href =地址。 手动测试,浏览器可以正确重定向。 我如何使用RSpec进行测试? response.should redirect_to失败
我在控制器问题中有一个动作,它包含在一个控制器中。 此操作不会呈现在respond_to块下指定的js.erb文件。 如何在控制器关注中正确获取操作以成功呈现js.erb文件(或任何视图)? 我的路线有问题吗? 模块操作的链接 = link_to image_tag(“upvote.png”), send(“vote_socionics_#{votable_name}_path”, votable, vote_type: “#{s.type_two_im_raw}”), id: “vote-#{s.type_two_im_raw}”, method: :post, remote: true **控制器动作的链接** = link_to “whatever”, characters_whatever_path, remote: true 控制器/ characters_controller.rb class CharactersController < ApplicationController include SocionicsVotesConcern def an_action respond_to do |format| format.js { render 'shared/vote_socionics' } # This renders/executes the file end end 控制器/关切/ socionics_votes_concern.rb module SocionicsVotesConcern extend […]
respond_to do |format| if @user.save format.js { render :nothing => true, :status => :ok, :location => @user } else format.js { render :json => @user.errors, :status => :unprocessable_entity } end end 我尝试过的所有选项(比如将respond_to :js放在控制器的顶部等)并没有像在这里那样工作。
利用ActionController的新的respond_with方法……当action(保存)成功时以及何时不成功时,它如何确定要呈现的内容? 我问,因为我试图获得一个脚手架生成的规范(包括在下面),如果只是为了让我能理解它。 该应用程序工作正常,但奇怪的是,当validation失败时,它似乎是渲染/carriers (至少是浏览器的URL所说的)。 然而,规范期待”new” (我也是如此),而是接收 。 如果我将规格改为期望””它仍然失败。 当它呈现/carriers ,该页面会显示错误validation字段旁边的错误消息。 任何熟悉respond_with人都可以看到这里发生了什么吗? #carrier.rb validates :name, :presence => true #carriers_controller.rb class CarriersController < ApplicationController respond_to :html, :json … def new respond_with(@carrier = Carrier.new) end def create @carrier = Carrier.new(params[:carrier]) flash[:success] = 'Carrier was successfully created.' if @carrier.save respond_with(@carrier) end 规格失败: #carriers_controller_spec.rb require ‘spec_helper’ describe CarriersController do def […]
我正在使用respond_with ,所有内容都正确连接以正确获取数据。 我想以干燥的方式自定义返回的json , xml和foobar格式,但我无法弄清楚如何使用有限的:only和:include 。 当数据很简单时,这些都很棒,但是对于复杂的发现,它们不符合我的要求。 可以说我有一个有很多图像的post def show @post = Post.find params[:id] respond_with(@post) end 我想用响应包含图像,所以我可以这样做: def show @post = Post.find params[:id] respond_with(@post, :include => :images) end 但我真的不想发送整个图像对象,只是url。 除此之外,我真的希望能够做到这样的事情(伪代码): def show @post = Post.find params[:id] respond_with(@post, :include => { :foo => @posts.each.really_cool_method } ) end def index @post = Post.find params[:id] respond_with(@post, :include => […]
我有一个活动资源,我查询数据。 它返回记录,计数,无论我要求什么。 例如:product = Product.find(123) 响应头应该包含一个自定义属性,比如“HTTP_PRODUCT_COUNT = 20”,我想检查响应。 IRB最有效的方法是什么? 我没有奢侈的Rails或其他可能提供底层响应的框架。 我是否需要使用monkeypatched调用或其他东西攻击Net :: HTTP或ActiveResource本身?
假设我有一个带有redirect_to_baz方法的FoosController 。 class FoosController < ApplicationController def redirect_to_baz redirect_to 'http://example.com/?foo=1&bar=2&baz=3' end end 我正在使用spec/controllers/foos_controller_spec.rb测试它: require ‘spec_helper’ describe FoosController, :type => :controller do describe “GET redirect_to_baz” do it “redirects to example.com with params” do get :redirect_to_baz expect(response).to redirect_to “http://example.com/?foo=1&bar=2&baz=3” end end end 有用。 但是,如果有人交换查询字符串参数(例如http://example.com/?bar=2&baz=3&foo=1 ),则测试失败。 测试这个的正确方法是什么? 我想做的事情如下: expect(response).to redirect_to(“http://example.com/”, params: { foo: 1, bar: 2, baz: 3 […]
我希望我的服务器发送多部分响应(multipart / x-mixed-replace)。 我更喜欢使用Sinatra框架或通用Rack应用程序的某种解决方案,但ruby中的任何示例都会很好。 这是我在PHP中尝试做的事情的等价物: <?php header('Content-type: multipart/x-mixed-replace;boundary="rn9012"'); print "–rn9012\n"; print "Content-type: application/xml\n\n"; print "\n”; print “First Part\n”; print “–rn9012\n”; flush(); sleep(5); print “Content-type: application/xml\n\n”; print “\n”; print “Second Part\n”; print “–rn9012–\n”; ?>