解决Fixnum错误

时间:2016-05-13 12:35:00

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

我有一个来自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

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

1 个答案:

答案 0 :(得分:2)

您正在employee_id上调用authorization_selected方法,这是一个字符串,但不提供此方法。 显然这不起作用。你可能想做

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

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

也许您可以更改表单,通过初始化程序分配值?