开发者

How to use ActiveResource in Rails application

开发者 https://www.devze.com 2023-03-26 06:56 出处:网络
I have Rails application (Redmine) and I have created next working code which close the Issue: require \'active_resource\'

I have Rails application (Redmine) and I have created next working code which close the Issue:

require 'active_resource'

class Issue < ActiveResource::Base
  self.site = "https://user@secret@esupport.some.com"
end

issue = Issue.find(7415)
issue.status_id = 5
issue.save

Now I want to put this code into Rails plugin. But if I put the class difinition to the Rails-plugins I get next error message:

TypeError (superclass mismatch fo开发者_如何学编程r class Issue):

I know the reason of the error - Rails application have a model Issue, but I don't know how to fix it.

If I'll change class difinition to

class **OtherIssue** < ActiveResource::Base
  self.site = "https://user@secret@esupport.some.com"
end

the ActiveResource doesn't know how to link my class to Rails model.


It's easy to forget you are working with xml when using ActiveResource If you had

class SomeOtherIssue < ActiveResource::Base
  self.site = "https://support.some.com"
  self.element_name = "site"
  self.user = "someone"
  self.password = "secret"
end

some_other_issue = SomeOtherIssue.find(7415)
some_other_issue.status_id = 5
some_other_issue.save

Then you would be saving the data back on the website. If you wanted to save the data on the Issue model locally you would have to find the local Issue record and assign the some_other_issue values to it

Update in response to comment

Use self.element_name = some path on the remote site e.g.

self.element_name = 'my_model' will navigate to

https://support.some.com/some_model

so when you call some_other_issue = SomeOtherIssue.find(7415)you will be navigating to the show action on the my_model controller and passing in 7415 as the id parameter. Because your remote site will be using a RESTfull route (I hope) you will get the xml response instead of the html response in the my_model/show action.

in your case you should set self.element_name = "issue". Hope that's clearer.

http://api.rubyonrails.org/classes/ActiveResource/Base.html will give you examples of this

0

精彩评论

暂无评论...
验证码 换一张
取 消