如何使用Mechanize或Faraday在Ruby中发送JSON表单数据

我想从使用JSON数据的网站检索数据,以设置似乎是通过AJAX请求的自定义搜索参数。 传输的数据显示在Firebug中的XHR-> Request Payload下: {“filters”: [{“action”: “post”, “filterName”: “Hersteller”, “ids”: [269], “settingName”: “Hersteller”, “settingValue”: “ValueA”}, {“action”: “delete”, “filterName”: “Modelle”, “settingName”: “Modelle”, “settingValue”: “”}]} 该站点不传输任何POST参数,只传输此JSON编码数据以应用搜索条件。 使用Mechanize将此数据作为post参数传递不起作用。 如何在Ruby on Rails中使用Mechanize或Faraday传输这些数据?

如何在Ruby中列出局部变量?

def method a = 3 b = 4 some_method_that_gives # [a, b] end

在本地安装ruby-debug19

我目前在Windows XP 32位机器上运行Ruby 1.9.2p0。 自从我们升级到Ruby 1.9以来,我无法进行任何forms的调试。 我被告知你需要新的ruby-debug19 gem才能让事情运行起来。 问题是,我公司的代理阻止了gem依赖管理器连接外部存储库。 没关系,我只是自己下载gem并在本地安装它。 结果: ERROR: Error installing ruby-debug19-0.11.6.gem: ERROR: Failed to build gem native extension. C:/Ruby192/bin/ruby.exe extconf.rb –with-ruby-include=C:\Ruby192\include checking for vm_core.h… no checking for vm_core.h… no *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log […]

如何rspec模拟ruby rails logger类

嗨我,并尝试rspec模拟以下类: class Person def initialize(personid) Rails.logger.debug “Creating person with id #{personid}” end end 使用这个: require ‘spec_helper’ describe Person do describe “#initialize” do let(:rails_mock) { double(“Rails”).as_null_object } let(:logger_mock) { double(“Rails.logger”).as_null_object } it “logs a message” do rails_mock.stub(:logger).and_return(logger_mock) logger_mock.should_receive(:debug) Person.new “dummy” end end end 并收到此消息: RSpec :: Mocks :: MockExpectationError 🙁 Double“Rails.logger”)。debug(any args) 预计:1次 收到:0次 任何帮助都会很棒!

Rake任务:error handling

我还在学习耙子。 Rake是否支持处理任务的错误,如NANT的MSBuild:如果此任务失败; 执行anoter任务(回滚等) 例如:在MSBuild中,它们具有OnError元素 谢谢你的帮助

如何在ruby中将类名作为变量传递

我有由另一个文件创建的foo.rb和main.rb文件。 foo.rb: class Foo def initialize @val = 1 end end main.rb的: file_name = gets.chomp() require_relative(file_name) class_name = file_name.capitalize a = class_name.new() p “This is val: #{a.val}” 但是我得到一个错误: undefined method new’for“Foo.rb”:String(NoMethodError)` 我的问题:如何将类名作为值传递。

Redis :: CommandError:ERR客户端发送了AUTH,但没有设置密码

这必须是一些错误的配置 我在localhost上安装了redis。 尝试通过Rails控制台连接到它时 Redis.new(:host => ‘localhost’, :port => 6379) 我无法发送命令而且我得到了 Redis::CommandError: ERR Client sent AUTH, but no password is set 我不记得设置密码,也不知道在哪里设置密码 如果我使用不同的URL(如rediscloud或redistogo),我可以发送命令(按照设置密码) 从我的理解错误说我发送密码,但redis没有密码。 但是,正如您所看到的,我没有设置任何密码.. 有没有办法调试这个?

I18n:使用’t(:test_key)’,’t(’test_key’)’和’t(’。test_key’)’有什么区别?

我正在使用Ruby on Rails 3.1,我想知道如何 , 何时以及为什么我应该使用以下代码而不是另一个代码来国际化我的应用程序( I18n gem ): t(:test_key) t(‘test_key’) t(‘.test_key’) 也就是说,使用t(:test_key) , t(‘test_key’)和t(‘.test_key’)之间的“微妙”区别是什么? 关于这个问题的最佳做法是什么?

Rspec – 未定义的方法’let’

这是我的rspec文件: require ‘spec_helper’ describe “Birds” do before { visit birds_path } it “should have the right title” do expect(page).to have_content(“Approved Birds”) end it “should contain the bird’s name, genus, species” do let(:bird) { FactoryGirl.create(:bird) } expect(page).to have_content(“#{bird.name}”) expect(page).to have_content(“#{bird.genus}”) expect(page).to have_content(“#{bird.species}”) end end 当我运行它时,我收到以下错误: Failure/Error: let(:bird) { FactoryGirl.create(:bird) } NoMethodError: undefined method `let’ for # […]

rails 3:我需要在before_save回调中返回true才能使object.save工作吗?

Class User before_save :set_searchable def set_searchable self.searchable = true if self.status == :active end end >> u = User.last >> u.save false u.save总是返回false。 如果我删除before_save它也有效,如果我在before_save中返回true它可以工作 所以我需要在before_save中给出return语句吗? 如果before_save返回false,ActiveRecord会保存一个对象吗? 我在哪里可以看到有关回调及其工作流程的完整文档。 提前致谢