Ruby on rails 参数超出创建操作的范围日期异常,但更新工作正常
我有一个表单,用户可以在其中输入日期(例如2014年3月18日): 当用户为一个新的Ruby on rails 参数超出创建操作的范围日期异常,但更新工作正常,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我有一个表单,用户可以在其中输入日期(例如2014年3月18日): 当用户为一个新的清单对象提交表单时,我在创建操作中不断得到超出范围的错误参数。但是,如果我已经有了一个现有的对象,并且我尝试更新它,它就可以正常工作。奇怪的是,我的puts没有输出服务器日志中的任何内容 def create puts "CREATE" # this line is not outputting anything purchase_date = inventory_params["purchase_d
清单
对象提交表单时,我在创建操作中不断得到超出范围的错误参数。但是,如果我已经有了一个现有的对象,并且我尝试更新它,它就可以正常工作。奇怪的是,我的puts没有输出服务器日志中的任何内容
def create
puts "CREATE" # this line is not outputting anything
purchase_date = inventory_params["purchase_date"].blank? ? Date.today : Date::strptime(inventory_params["purchase_date"], "%m/%d/%Y")
@inventory = Inventory.create( name: inventory_params["name"],
purchase_price: purchase_price,
purchase_date: purchase_date,
serial: inventory_params["serial"].to_s,
description: inventory_params["description"].to_s,
product_type: inventory_params["product_type"].to_s,
current_location: inventory_params["current_location"].to_s,
vendor_id: inventory_params["vendor_id"],
team_id: current_team.id)
redirect_to inventories_path
end
def update
purchase_date = inventory_params["purchase_date"].blank? ? Date.today : Date::strptime(inventory_params["purchase_date"], "%m/%d/%Y")
@inventory.update_attributes( name: inventory_params["name"],
purchase_price: purchase_price,
purchase_date: purchase_date,
serial: inventory_params["serial"],
description: inventory_params["description"],
product_type: inventory_params["product_type"],
current_location: inventory_params["current_location"],
vendor_id: inventory_params["vendor_id"]
)
redirect_to inventory_path(@inventory)
end
private
def inventory_params
params.require(:inventory).permit(:purchase_price, :purchase_date, :name, :serial, :description, :product_type, :current_location, :vendor_id)
end
我也设置了CanCan,但我不认为这会影响它:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
can [:create, :show, :destroy, :edit, :update], User, id: user.id
can :create, Inventory
can [:index, :show, :destroy, :edit, :update], Inventory do |inventory|
user && (inventory.try(:team_id) == user.teams.first.id)
end
end
end
有人知道为什么我在尝试创建库存的新实例时总是遇到异常吗
更新:看起来,如果我的约会使用两位数,它就会中断。例如,2014年3月1日
起作用,但2014年3月11日
中断 只是为了你下次少写几行。您编写的内容相当于@inventory=
Inventory.create(inventory_params.merge({team_id: current_team.id}))
及
还有,我发现了你的错误,你写了03/18/14(也就是说2003-18-14),没有第18个月!
它不是用法国标准写的:)
顺便说一句,如果你需要使用法语(或类似的)标准:请阅读
并在此处下载必要的文件:
Cancan正在使用Rails 4.0进行破坏。
这解决了问题:
加载和授权资源:除了=>[:创建]
供应商id
和序列
字段的类型是什么?那个错误可能是数据库引发的?就像int
/bigint
问题一样?顺便问一下,你不使用强参数吗?您确定正确接收表单值吗?尤其是inventory\u params[“purchase\u date”]
?@scaryguy vendor\u id是一个int,serial是一个字符串。使用put
进行调试不是很有用。我建议查看pry
gem(pry-rails
如果在rails项目中)。它可以帮助您更好地调试。它将作为代码的断点,并允许您查看该点中所有内容的值。我有%m/%d/%Y”
,所以2014年3月18日
不应该工作吗?我认为我没有使用法国标准。如果我尝试了2014年3月18日
,那么我得到了无效的日期异常。您在哪里找到了%m/%d/%Y”
?要检查它是否工作,请在rails控制台中尝试Time.parse“03/18/2014”
。purchase\u date=inventory\u params[“purchase\u date”]。空白?Date.today:Date::strTime(库存参数[“采购日期],%m/%d/%Y”)
是的,我就是这么告诉你的!法国标准为dd/mm/yyyy。国际(默认值)是yyyy/mm/dd。我还遇到了一个问题,CanCanCan在保存之前在控制器中处理的datetime值出现故障。在运行CanCan之前,我使用了一个before_过滤器来处理该值,这解决了这个问题,并允许我在create上运行CanCan。
Inventory.create(inventory_params.merge({team_id: current_team.id}))
@inventory.update_attributes(inventory_params)