参数超出范围日期创建操作的异常但更新工作正常

时间:2014-03-26 01:33:14

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个表格,用户可以输入日期(例如03/18/2014):

= f.text_field :purchase_date, class: "form-control", placeholder: "mm/dd/yyyy"

当用户提交新Inventory对象的表单时,我在创建操作中不断收到错误argument out of range。但是,如果我已经有一个现有的对象,并且我尝试更新它,它可以正常工作。奇怪的是,我的看跌期权并没有在服务器日志中输出任何内容。

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

当我尝试创建Inventory的新实例时,有谁知道为什么我会一直收到异常?

更新:如果我的日期使用两位数,它就会中断。例如,3/1/2014有效,但3/11/2014会中断......

2 个答案:

答案 0 :(得分:1)

下次你只需要少写几行。你写的内容相当于@inventory =

Inventory.create(inventory_params.merge({team_id: current_team.id}))

@inventory.update_attributes(inventory_params)

另外,我发现你的错误,你写的是03/18/14(那意味着2003-18-14),没有18个月! 它不是用法语标准写的:)

顺便说一下,如果你需要使用法语(或类似)标准:请阅读 http://guides.rubyonrails.org/i18n.html

并在那里下载必要的文件: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/fr.yml

答案 1 :(得分:1)

Cancan打破了Rails 4.0。 这解决了这个问题:

load_and_authorize_resource :except => [:create]