使用值在Ruby中设置对象的键

时间:2021-05-15 21:06:36

Inside a loop, I'm trying to set the value as the object's key. Not sure how to do this:

在循环中,我试图将值设置为对象的键。不知道怎么做:

@user = {"username"=>"123", "full_name"=>"John Doe"}

@account = Account.new
@user.keys.each { |key|
  @account.key = @user[key]
}

That returns an error of: NoMethodError: undefined method `key='

返回错误:NoMethodError:undefined method`key ='

3 个解决方案

#1


3  

Function calls in Ruby act like message-passing. So what you are looking for is send.

Ruby中的函数调用就像消息传递一样。所以你要找的是发送。

The @account object in your example is not a Hash like @user, it is a Class instance.

示例中的@account对象不是像@user这样的Hash,它是一个Class实例。

But Rails has an even better way to initialize a Model with attributes:

但是Rails有一种更好的方法来初始化具有属性的Model:

@account = Account.new(username: '123', full_name: 'John Doe')

In a Rails controller, these are usually in the params hash if you wrote your form correctly:

在Rails控制器中,如果您正确地编写了表单,这些通常都在params哈希中:

@account = Account.new(params[:user])

If you must do it manually, you can:

如果必须手动执行,您可以:

@account = Account.new

# set all attributes at once
@account.attributes = @user

# this also works, but it's the least desirable
@user.each {|key, value| @account.send("#{key}=", value) }

The last example works because @account.key = value is actually syntactic sugar for a method call: @account.key=(value)

最后一个例子有效,因为@ account.key = value实际上是方法调用的语法糖:@ account.key =(value)

I highly recommend reading through Rails Form Helpers and building your forms how it suggests.

我强烈建议您阅读Rails Form Helpers并构建表单。

#2


0  

It looks like what you want is to make @account.username be 123, and @account.full_name be "John Doe", right?

看起来你想要的是将@ account.username设为123,将@ account.full_name设为“John Doe”,对吗?

Does Account have a username and fullname attribute? Does it have a key attribute? It obviously don't have key, because that's why you're getting that error.

Account是否具有用户名和fullname属性?它有关键属性吗?它显然没有密钥,因为这就是你得到这个错误的原因。

If Account does have username and fullname, then just do:

如果Account确实有用户名和全名,那么就这样做:

@account.update_attributes!(@user)

If it doesn't, then I have no idea what you're trying to do.

如果没有,那么我不知道你想做什么。

#3


0  

Do it this way;

这样做吧;

@user = {"username" => "123", "full_name" => "John Doe"}
@account = Account.new
@user.keys.each{|key| eval("@account.#{key} = @user[key]")}

#1


3  

Function calls in Ruby act like message-passing. So what you are looking for is send.

Ruby中的函数调用就像消息传递一样。所以你要找的是发送。

The @account object in your example is not a Hash like @user, it is a Class instance.

示例中的@account对象不是像@user这样的Hash,它是一个Class实例。

But Rails has an even better way to initialize a Model with attributes:

但是Rails有一种更好的方法来初始化具有属性的Model:

@account = Account.new(username: '123', full_name: 'John Doe')

In a Rails controller, these are usually in the params hash if you wrote your form correctly:

在Rails控制器中,如果您正确地编写了表单,这些通常都在params哈希中:

@account = Account.new(params[:user])

If you must do it manually, you can:

如果必须手动执行,您可以:

@account = Account.new

# set all attributes at once
@account.attributes = @user

# this also works, but it's the least desirable
@user.each {|key, value| @account.send("#{key}=", value) }

The last example works because @account.key = value is actually syntactic sugar for a method call: @account.key=(value)

最后一个例子有效,因为@ account.key = value实际上是方法调用的语法糖:@ account.key =(value)

I highly recommend reading through Rails Form Helpers and building your forms how it suggests.

我强烈建议您阅读Rails Form Helpers并构建表单。

#2


0  

It looks like what you want is to make @account.username be 123, and @account.full_name be "John Doe", right?

看起来你想要的是将@ account.username设为123,将@ account.full_name设为“John Doe”,对吗?

Does Account have a username and fullname attribute? Does it have a key attribute? It obviously don't have key, because that's why you're getting that error.

Account是否具有用户名和fullname属性?它有关键属性吗?它显然没有密钥,因为这就是你得到这个错误的原因。

If Account does have username and fullname, then just do:

如果Account确实有用户名和全名,那么就这样做:

@account.update_attributes!(@user)

If it doesn't, then I have no idea what you're trying to do.

如果没有,那么我不知道你想做什么。

#3


0  

Do it this way;

这样做吧;

@user = {"username" => "123", "full_name" => "John Doe"}
@account = Account.new
@user.keys.each{|key| eval("@account.#{key} = @user[key]")}