-6

我有一个来自 URL 的参数 (params[:authorization]),如您所见:

authorization_selected = params[:authorization]
new_parcel = params[:new_parcel].to_i

puts authorization_selected.class (in the console show type String)
puts new_parcel.class (in the console show type Fixnum)

在我的控制器中,有:

@portability = Portability.new
@portability.employee_id = authorization_selected.employee_id

但是,这会返回一个错误:

undefined method `employee_id' for 3:Fixnum

我需要两者都是整数。怎么做?

4

1 回答 1

2

您正在调用的employee_id方法authorization_selected是 String 并且不提供此方法。显然这是行不通的。你可能想做

@portability = Portability.new
@portability.employee_id = authorization_selected

假设params[:employee]包含employee_idandPortability是 ActiveModel 或 ActiveRecord。

也许您可以更改可以通过初始化程序分配值的形式?

于 2016-05-13T12:41:09.710 回答