用简单的控制器:
def new
@product = Product.new
respond_to do |format|
format.html #new.html.erb
format.json { render json: @product}
end
end
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: "Save process completed!" }
format.json { render json: @product, status: :created, location: @product }
else
format.html {
flash.now[:notice]="Save proccess coudn't be completed!"
render :new
}
format.json { render json: @product.errors, status: :unprocessable_entity}
end
end
end
Run Code Online (Sandbox Code Playgroud)
和简单的ajax请求
$("h1").click ->
$.post
url: "/products/"
data:
product:
name: "Filip"
description: "whatever" …Run Code Online (Sandbox Code Playgroud) 我工作FactoryGirl在version 4.4.我正在尝试创建after(:create)回调,但我在评估变量上失败了.
FactoryGirl.define do
factory :organization do
name 'example'
factory :organization_with_links do
transient do
links_count 5
end
after(:create) do |organization, evaluator|
create_list(:link, evaluator.links_count, organization: organization)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
不幸的是我得到了
NoMethodError: undefined method `links_count' for FactoryGirl::SyntaxRunner:0x007fcefb1ff780>
Run Code Online (Sandbox Code Playgroud)
根据官方工厂女孩指南我正在做的一切正确:https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations
任何建议如何transient variable工作?
lodash中有_.merge功能.我想在ES6或ES7中实现同样的功能.
有这个片段:
Object.assign({}, {key: 2}, {key: undefined})
Run Code Online (Sandbox Code Playgroud)
我想收到{key: 2}.目前我收到了{key: undefined}
这不是深度合并.
可能吗?如果是,那么如何实现呢?
如何通过传递哈希来构造带有查询参数的URI对象?
我可以生成查询:
URI::HTTPS.build(host: 'example.com', query: "a=#{hash[:a]}, b=#{[hash:b]}")
产生
https://example.com?a=argument1&b=argument2
但是我认为为许多参数构造查询字符串将是不可读的并且难以维护.我想通过传递hash来构造查询字符串.如下例所示:
hash = {
a: 'argument1',
b: 'argument2'
#... dozen more arguments
}
URI::HTTPS.build(host: 'example.com', query: hash)
Run Code Online (Sandbox Code Playgroud)
提升了
NoMethodError: undefined method `to_str' for {:a=>"argument1", :b=>"argument2"}:Hash
Run Code Online (Sandbox Code Playgroud)
是否可以使用URI api构建基于哈希的查询字符串?我不想猴子补丁哈希对象...
我正在测试我的模块,我决定测试它与匿名类:
subject(:klass) { Class.new { include MyModule } }
Run Code Online (Sandbox Code Playgroud)
MyModule使用name里面的方法klass.为了让我的规范工作,我需要存根这个方法name(未实现).所以我写道:
subject { klass.new }
allow(subject).to receive(:name).and_return('SOreadytohelp') }
Run Code Online (Sandbox Code Playgroud)
但它引起了:
RSpec::Mocks::MockExpectationError: #<#<Class:0x007feb67a17750>:0x007feb67c7adf8> does not implement: name
from spec-support-3.3.0/lib/rspec/support.rb:86:in `block in <module:Support>'
Run Code Online (Sandbox Code Playgroud)
如何在不定义的情况下存根此方法?
假设我有一个方法#sum,它接受一个数组并计算所有元素的总和.我正在抄袭它:
before do
expect(calculation_service).to receive(:sum?).with([1, 2, 3]) { 6 }
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,我的测试服以随机顺序传递数组.由于该错误被提出:
Failure/Error: subject { do_crazy_stuff! }
#<InstanceDouble() (CalculationService)> received :sum? with unexpected arguments
expected: ([1, 2, 3])
got: ([3, 2, 1])
Run Code Online (Sandbox Code Playgroud)
是否有可能对方法调用忽略数组元素的顺序?array_including(1, 2, 3)不确定数组大小,所以它可能不是最好的解决方案
是否可以将sidekiq作业从SidekiqWorker实例级别直接移动到死队列(即执行时)
class MyWorker
include Sidekiq::Worker
sidekiq_options retry: 9
def perform(name)
if name == 'StackOverflow'
# ----> skip_retry_queue_and_go_to_dead_queue
else
# do_stuff!
end
end
end
Run Code Online (Sandbox Code Playgroud) 你可以改进你的课程
module RefinedString
refine String do
def to_boolean(text)
!!(text =~ /^(true|t|yes|y|1)$/i)
end
end
end
Run Code Online (Sandbox Code Playgroud)
但如何细化模块方法?这个:
module RefinedMath
refine Math do
def PI
22/7
end
end
end
Run Code Online (Sandbox Code Playgroud)
提出: TypeError: wrong argument type Module (expected Class)
@var_name在 ruby 中,您可以直接通过或通过 private getters在内部访问变量attr_reader :var_name。
哪种解决方案更(语义上?)正确?使用解决方案 1 或解决方案 2 有什么优点/缺点?
解决方案一:
class Point
def initialize(x, y)
@x = x
@y = y
end
def distance
Math.sqrt(@x ** 2 + @y ** 2)
end
end
Run Code Online (Sandbox Code Playgroud)
解决方案2:
class Point
def initialize(x, y)
@x = x
@y = y
end
def distance
Math.sqrt(x ** 2 + y ** 2)
end
private
attr_reader :x, :y
end
Run Code Online (Sandbox Code Playgroud) def up; end和之间有什么区别def change; end?我有代码
class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.string :status
t.integer :zombie_id
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
如果我定义def up而不是改变它会改变什么def change?