Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Activeresource自定义方法:id规范_Ruby On Rails_Ruby On Rails 3_Activeresource - Fatal编程技术网

Ruby on rails Activeresource自定义方法:id规范

Ruby on rails Activeresource自定义方法:id规范,ruby-on-rails,ruby-on-rails-3,activeresource,Ruby On Rails,Ruby On Rails 3,Activeresource,我需要/想要使用activeresource设置一个自定义方法 myresource.put(:go_crazy, {:go => "go", :crazy => "crazy"}) 这将转到资源所在的服务器上 /myresource/:id(pretend is the integer 1)/go_crazy?go=go%crazy=crazy 只是,我需要指定:id是其他内容,例如“myresource.crazy_key” 实际上,我已经找到了另一种方法来解决这个问题,但是

我需要/想要使用activeresource设置一个自定义方法

myresource.put(:go_crazy, {:go => "go", :crazy => "crazy"})
这将转到资源所在的服务器上

/myresource/:id(pretend is the integer 1)/go_crazy?go=go%crazy=crazy
只是,我需要指定:id是其他内容,例如“myresource.crazy_key”

实际上,我已经找到了另一种方法来解决这个问题,但是如果我可以这样做的话,我似乎不知道我需要做什么来将id指定为其他东西。

在“资源”模型中,重写
to_param
方法

def to_param
    crazy_key.to_s
end
在“资源”模型中,重写
to_param
方法

def to_param
    crazy_key.to_s
end

我假设您想使用其他内容,而不是URL中的id。然后,您必须定义将作为活动资源类主键的属性:

class FooBar < ActiveResource::Base
    self.site="http://host/root/"
    self.primary_key = :name
end
将要求

http://host/root/foo_bar/XYZ/subfoo

我假设您想使用其他内容,而不是URL中的id。然后,您必须定义将作为活动资源类主键的属性:

class FooBar < ActiveResource::Base
    self.site="http://host/root/"
    self.primary_key = :name
end
将要求

http://host/root/foo_bar/XYZ/subfoo

它在前面已经存在,我使用该键作为AR的id,但不是因为从前面已经存在,我使用该键作为AR的id,但不是因为