0

我的控制器中有一个哈希数据:

 "accept"=>["{:id=>\"12f310f1d9b8f\",
:first_name=>\"San\",
:last_name=>\"Jori\",
 :name=>\"Jori,Santosh\",
 :email=>\"san.jori@west.com\",
 :gender=>nil,
:birthday=>nil,
:profile_picture=>nil,
:relation=>nil}"],

但我无法阅读它。

我正在尝试以这种方式阅读它:

  if params[:accept].present?
   params[:accept].each do |customer|
   @customer = current_user.customers.new(:name => customer[:name], :email => customer[:email])
   end
  end

但它给出了错误:

  no implicit conversion of Symbol into Integer

蚂蚁有人帮忙吗?请。

4

1 回答 1

0

首先,哈希名称accept不在customer您的示例中。那是从更大的变量中提取的吗?是参数哈希吗?

其次,它是一个哈希数组,因此您需要从数组中获取一个哈希结果集才能使用。你是怎么做到的?您的示例中的客户对象是什么?这可能是您的错误的原因,您正在尝试访问哈希样式键,但您正在对数组执行此操作,这需要一个 int 键来访问特定成员,如下所示:customer = accept[0] # the first hash in the array或内联假设customer== accept

@customer = current_user.customers.new(:name => customer[0][:name], :email => customer[0][:email])

也就是说,如果所有哈希键都与客户对象属性匹配,那么您可以直接将哈希传递给customers.new方法:

@customer = current_user.customers.new(accept)

这通常在控制器create操作中完成,如下所示:

def create
  @customer = Customer.new(params[:customer])
  ...
end
于 2013-06-24T10:25:15.113 回答