【发布时间】:2018-06-16 15:25:30
【问题描述】:
我目前正在制作一个租赁系统,在该系统中,我需要使用 RoR 为何时租赁以及何时归还租赁物品创建一个时间。我的代码将时间保存在数据库中,然后回滚错误value required 2, got nil。
以下是我的代码:
AcquiretimesController < ApplicationController
def create
@item = Item.find(params[:item_id])
@acquiretime = @item.acquiretimes.create(time_params)
if @acquiretime.valid?
flash[:success] = "You have successfully rented the item"
redirect_to root_path
else
flash[:alert] = "Woops! Looks like there has been error. Please enter valid data."
redirect_to root_path
end
end
private
def time_params
params.require(:acquiretime).permit(:required_time, :return_time)
end
在我看来,我使用了以下内容:
<%= simple_form_for [current_item, @acquiretime] do |f| %>
<div class = "add_item_form">
<%= f.input :required_time, as: :date, html5: true %>
<%= f.input :return_time, as: :date, html5: true %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit 'Add Item', class: 'btn btn-primary' %>
</div>
<% end %>
我的 Acquiretime 模型是:
class Acquiretime < ApplicationRecord
belongs_to :item
belongs_to :users
validates :required_time, :return_time, presence: true
end
对于物品,
class Item < ApplicationRecord
belongs_to :user
has_many :acquiretimes
end
还有用户,
class User < ApplicationRecord
has_many :items
has_one :acquiretime
end
以下是我在控制台中的错误消息:
在 2018-01-07 18:28:04 +0545 为 127.0.0.1 开始 POST "/items/3/acquiretimes" AcquiretimesController#create as HTML 处理 参数:{"utf8"=>"✓", "authenticity_token"=>"Zo9qj1VsLlMwgwSozATh2C//tMQHbFOaMvZ1IddeVdQxG7enNDmPWe64RO/1CmmpBlevmSfJ7/w9Fzey5NxP8A==", "acquiretime"=>{"required_time"=>"2018-01- return_time"=>"2018-01-22"}, "commit"=>"添加项目", "item_id"=>"3"} 项目加载 (0.3ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2 [["id", 3], ["LIMIT", 1]] (0.2ms) 开始 (0.2ms) 提交 (0.1ms) 开始 (0.1ms) 回滚 在 117 毫秒内完成 500 内部服务器错误(ActiveRecord:1.0 毫秒)
ArgumentError(参数数量错误(给定 0,预期为 2)): app/controllers/acquiretimes_controller.rb:18:in `create' 在救援中渲染 /home/skj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb /布局 渲染/home/skj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb 渲染 /home/skj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.7小姐) 渲染/home/skj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb 渲染 /home/skj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.7小姐) 渲染/home/skj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb 渲染 /home/skj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.7小姐) 在救援中渲染 /home/skj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb /布局(107.9 毫秒)
【问题讨论】:
-
发生回滚时能否发布控制台输出?
-
@kuwantum,我添加了错误,请看一下
-
app/controllers/acquiretimes_controller.rb(第 18 行)- 那一行是什么?好像有问题
-
这是警报信息部分
-
你能发布你的模型代码(我想是 Aquiretime)吗?
标签: ruby-on-rails