我有一个Position带有字段started_at和的模型ended_at,这两个都是datetime字段。因为两者的日期部分必须相同,所以我有一个datetime_select表单字段 forstarted_at但只有一个time_select ignore_date: truefor ended_at。在create我想将日期值分配给这样的方法started_at中ended_at:
params[:position]["ended_at(1i)"] = params[:position]["started_at(1i)"]
params[:position]["ended_at(2i)"] = params[:position]["started_at(2i)"]
params[:position]["ended_at(3i)"] = params[:position]["started_at(3i)"]
但这打破了我的控制器规范,因为我在那里直接分配了值:
describe "POST create" do
describe "with valid params" do
it "creates a new Position" do
expect {
post :create, {:position => valid_attributes}, valid_session
}.to change(Position, :count).by(1)
end
end
end
所以我最终得到了这个,但我不确定这是否是一个合理的修复:
if params[:position]["ended_at(1i)"].nil? and not params[:position]["started_at(1i)"].nil? # We have to check this because in the controller specs we assign the params directly (not in this crazy xxx_at(yi) form for dates), so by checking this we know that the data was sent through the form
params[:position]["ended_at(1i)"] = params[:position]["started_at(1i)"]
params[:position]["ended_at(2i)"] = params[:position]["started_at(2i)"]
params[:position]["ended_at(3i)"] = params[:position]["started_at(3i)"]
end
也许有更好的解决方案?感谢您的意见。