标签: activeresource

是否有类似Java的ActiveResource?

我知道有一些Java的ActiveResource 客户端库,比如RAPAJactiveResource

问题是:有没有简单的方法在Java中创建ActiveResource 的服务器端

在RubyOnRails中,您需要做的就是使用Inherited Resources gem.Java中有类似的东西吗?

java rest ruby-on-rails activeresource inherited-resources

8
推荐指数
1
解决办法
607
查看次数

如何查看对ActiveResource请求的HTTP响应?

我正在尝试调试无效的ActiveResource调用.

查看ActiveResource请求的HTTP响应的最佳方法是什么?

ruby ruby-on-rails activeresource

7
推荐指数
2
解决办法
6376
查看次数

HTTP POST中的多个键/值对,其中键的名称相同

我正在开发一个接受远程客户端数据的API,其中一些HTTP POST中的密钥几乎就像一个数组.在英语中,这意味着我的服务器上有一个名为"class"的资源.在这个意义上的一个类是学生所在的类型和教师教育的类型.当用户提交HTTP POST为他们的应用程序创建一个新类时,许多键值对看起来像:

student_name:Bob Smith
student_name:Jane Smith
student_name:Chris Smith

什么是在客户端处理这个问题的最佳方法(假设客户端是cURL或ActiveResource,无论如何......)如果我的服务器是Ruby on Rails应用程序,那么在服务器端处理这个问题的方法是什么?需要一种方法来允许具有相同名称的多个密钥,并且没有任何命名空间冲突或丢失数据.

我的要求必须是POST数据是urlencoded键/值对.

ruby rest ruby-on-rails activeresource

7
推荐指数
1
解决办法
5504
查看次数

获取ActiveModel :: Callbacks以使用ActiveResource

我试图让ActiveModel :: Callbacks与ActiveResource(特别是after_initialize)一起使用Rails 3应用程序,但我似乎无法让它工作.我没有得到任何错误,但回调方法永远不会执行.

这是一段代码

class User < ActiveResource::Base
  extend ActiveModel::Callbacks
  define_model_callbacks :initialize, :only => :after

  after_initialize :update_info

  def update_info
    puts 'info'
  end 
end
Run Code Online (Sandbox Code Playgroud)

出于某种原因,从不执行update_info.任何人都知道如何让这个工作?

activeresource activemodel ruby-on-rails-3

7
推荐指数
1
解决办法
2126
查看次数

Rails:ActiveResource - 我可以明确设置ID吗?

我在Rails中使用ActiveResource来管理REST上单独数据库中的实体.

我一直在尝试显式管理远程资源的ID,因为在当前情况下,最简单的方法是重新使用本地资源的ID,而不是维护不同的字段.

不幸的是我一直没能得到这个工作作为代码new?ActiveResource::Base

  def new?
    id.nil?
  end
Run Code Online (Sandbox Code Playgroud)

并且save

 def save
   new? ? create : update
 end
Run Code Online (Sandbox Code Playgroud)

因此,根据定义,不可能设置资源的ID并将其保存为新资源.

create并且update是受保护的方法,所以如果我攻击ActiveResource :: Base代码可能会使它工作,但我不愿意这样做.

这样做有正确的方法吗?或者我正在努力做的只是坏事,我不应该这样做?

rest ruby-on-rails activeresource

6
推荐指数
1
解决办法
6785
查看次数

Rails ActiveResource关联

我有一些ARes模型(见下文),我正在尝试使用关联(这似乎完全没有文档,也许不可能,但我想我会尝试一下)

所以在我的服务方面,我的ActiveRecord对象将呈现类似的东西

render :xml => @group.to_xml(:include => :customers)
Run Code Online (Sandbox Code Playgroud)

(参见下面生成的xml)

模型组和客户是HABTM

在我的ARes方面,我希望它可以看到<customers>xml属性并自动填充该.customersGroup对象的属性,但是不支持has_many等方法(至少据我所知)

所以我想知道ARes如何反映XML来设置对象的属性.例如,在AR中,我可以def customers=(customer_array)自己创建并设置它,但这似乎在ARes中不起作用.

我找到一个"关联"的建议就是有一个方法

def customers
  Customer.find(:all, :conditions => {:group_id => self.id})
end
Run Code Online (Sandbox Code Playgroud)

但这有一个缺点,就是它会进行第二次服务电话来查找这些客户......并不酷

我希望我的ActiveResource模型能够看到XML中的客户属性并自动填充我的模型.有人对此有经验吗??

# My Services
class Customer < ActiveRecord::Base
  has_and_belongs_to_many :groups
end

class Group < ActiveRecord::Base
  has_and_belongs_to_many :customer
end

# My ActiveResource accessors
class Customer < ActiveResource::Base; end
class Group < ActiveResource::Base; end

# XML from /groups/:id?customers=true

<group>
  <domain>some.domain.com</domain>
  <id type="integer">266</id>
  <name>Some Name</name>
  <customers type="array">
    <customer>
      <active type="boolean">true</active>
      <id type="integer">1</id>
      <name>Some …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails associations activeresource

6
推荐指数
1
解决办法
6552
查看次数

使用ActiveResource保存时,"为数据分配器未定义"

我错过了什么?我正在尝试使用Active资源的休息服务,我有以下内容:

class User < ActiveResource::Base
  self.site = "http://localhost:3000/"
  self.element_name = "users"
  self.format = :json
end

user = User.new(
        :name => "Test",
        :email => "test.user@domain.com")

p user 
if user.save
  puts "success: #{user.uuid}"
else
  puts "error: #{user.errors.full_messages.to_sentence}"
end
Run Code Online (Sandbox Code Playgroud)

并为用户输出以下内容:

#<User:0x1011a2d20 @prefix_options={}, @attributes={"name"=>"Test", "email"=>"test.user@domain.com"}>
Run Code Online (Sandbox Code Playgroud)

而这个错误:

/Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1233:in `new': allocator undefined for Data (TypeError)
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1233:in `load'
from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1219:in `each'
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1219:in `load'
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1322:in `load_attributes_from_response'
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1316:in `create_without_notifications'
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1314:in `tap'
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1314:in `create_without_notifications'
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/observing.rb:11:in `create'
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/base.rb:1117:in `save_without_validation'
    from /Library/Ruby/Gems/1.8/gems/activeresource-3.0.10/lib/active_resource/validations.rb:87:in `save_without_notifications'
    from …
Run Code Online (Sandbox Code Playgroud)

ruby activeresource

6
推荐指数
1
解决办法
3660
查看次数

调试activeresource

我正在尝试使用Freebase API获得activeresource(在Rails 3.2上),但我还没有太多运气.如何调试rails以查看正在发生的事情并确保请求格式正确?

我认为后缀.json导致失败,但我不知道如何检查发生了什么?

错误:

ActiveResource::ResourceNotFound: Failed.  Response code = 404.  Response message = Not Found.
Run Code Online (Sandbox Code Playgroud)

码:

class Freebase < ActiveResource::Base
  class << self # also tried without this, same result
    def element_path(id, prefix_options = {}, query_options = nil)
      prefix_options, query_options = split_options(prefix_options) if query_options.nil?
      "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
    end

    def collection_path(prefix_options = {}, query_options = nil)
      prefix_options, query_options = split_options(prefix_options) if query_options.nil?
      "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
    end
  end


  self.site = "https://www.googleapis.com/freebase/v1/"
  self.format = :json

  #https://www.googleapis.com/freebase/v1/search?query=nirvana&indent=true

  #Freebase.get('search', :query => 'nirvana')



end
Run Code Online (Sandbox Code Playgroud)

更新:

好的,我发现有两件事情出错......

1)我试图取代的collection_path根本不起作用..它仍然将.json附加到每个请求上.2)https://www.googleapis.com:443/freebase/v1/freebases/search.json?query=nirvana 它后来解决了freebase …

rest freebase ruby-on-rails activeresource ruby-on-rails-3

6
推荐指数
2
解决办法
5761
查看次数

配置ActiveResource以支持OAuth2

我需要能够配置ActiveResource以连接为基础连接OAuth2或基本身份验证.我找到了几种使用OAuth2配置ActiveResource的方法,但它们看起来并不优雅,也不适合动态类型配置.有帮助吗?

ruby-on-rails activeresource basecamp 37-signals oauth-2.0

6
推荐指数
1
解决办法
1081
查看次数

使用ActiveResource模型Rails 4时,嵌套表单失败

我在为ActiveResource模型创建表单时遇到问题,但我似乎无法找到解决方案.问题是模型不知道字段,并在我尝试创建文本字段时抛出错误:

undefined method `firstname' for #<User:0x00000002946ec8>
Run Code Online (Sandbox Code Playgroud)

对于这种形式:

<%= form_for(@user) do |f| %>
<div class="field">
   <%= f.label :firstname %><br>
   <%= f.text_field :firstname %>
 </div>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

def new
  @user = User.new
end
Run Code Online (Sandbox Code Playgroud)

这是我的用户模型:

class User < ActiveResource::Base
  self.site = "https://***.com/api/v1.0/"
end
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法,但是这对ActiveResource模型不起作用,它不知道再也无法存储检索到的数据.user.firstname为空,当我删除它不是行...

attr_accessor :firstname, :lastname
Run Code Online (Sandbox Code Playgroud)

然后我找到了宝石Fortify(https://rubygems.org/gems/fortify),但最后一次更新是在2010年,安装它不起作用......

我希望有人熟悉这个问题,可以帮助我朝着正确的方向前进.

ruby ruby-on-rails activeresource

6
推荐指数
1
解决办法
720
查看次数