1

我正在尝试通过 REST API 使用 Redmine,以保持和更新项目成员资格,如http://www.redmine.org/projects/redmine/wiki/Rest_Memberships中所述

为了做到这一点,我在我的客户端应用程序中包含了用 RoR 编写的以下“redmine_project”模型:

class RedmineProject < ActiveResource::Base
  self.site ='http://localhost:3001'
  self.format = :json
  self.user = 'admin'
  self.password = 'admin'
  self.element_name = 'project'
  self.collection_name = 'projects'

end

然后,在我的控制台中,我尝试使用以下命令包含一个新成员:

RedmineProject.find(4).post(:memberships,:user_id => 12, :role_ids => [4])

推荐的第一部分由 Redmine 正确处理(实际上,如果我只是尝试 RedmineProject.find(4) 我确实会得到一个带有我的项目数据的 JSON)但是.. 我无法发布新成员资格

.post(:memberships,:user_id => 12, :role_ids => [4])

这是我的应用程序控制台的结果:

ActiveResource::ServerError: Failed.  Response code = 500.  Response message = Internal Server Error .
    from /home/marc/.rvm/gems/ruby-1.9.3-p448/gems/activeresource-3.2.13/lib/active_resource/connection.rb:148:in `handle_response'

这是 redmine 终端的结果:

Started POST "/projects/4/memberships.json?role_ids%5B%5D=4&user_id=12" for 127.0.0.1 at 2013-10-24 21:32:41 +0200
Processing by MembersController#create as JSON
  Parameters: {"project"=>{"created_on"=>"2013-10-23T00:58:15Z", "description"=>"azfazfazf zaf azffza", "homepage"=>"", "id"=>4, "identifier"=>"team-35", "name"=>"azfazf", "updated_on"=>"2013-10-23T00:58:15Z"}, "role_ids"=>["4"], "user_id"=>"12", "project_id"=>"4"}
WARNING: Can't verify CSRF token authenticity
   (0.6ms)  SELECT MAX(`settings`.`updated_on`) AS max_id FROM `settings` 
  User Load (0.9ms)  SELECT `users`.* FROM `users` WHERE `users`.`type` IN ('User', 'AnonymousUser') AND `users`.`login` = 'admin'
  SQL (95.6ms)  UPDATE `users` SET `last_login_on` = '2013-10-24 21:32:41' WHERE `users`.`type` IN ('User', 'AnonymousUser') AND `users`.`id` = 1
  Current user: admin (id=1)
  Project Load (0.4ms)  SELECT `projects`.* FROM `projects` WHERE `projects`.`id` = 4 LIMIT 1
  EnabledModule Load (0.4ms)  SELECT name FROM `enabled_modules` WHERE `enabled_modules`.`project_id` = 4
Completed 500 Internal Server Error in 127ms

NoMethodError (undefined method `valid?' for nil:NilClass):
  app/controllers/members_controller.rb:70:in `block (2 levels) in create'
  app/controllers/members_controller.rb:65:in `create'

我的代码有问题吗?或者它是一个(不太可能的)错误?

4

1 回答 1

0

我假设您使用 Redmine v2.3

查看引发错误的地方,我看到那members是空数组!查看构建过程,members我可以假设它params[:membership]是空白的,请查看您的日志:

{"project"=>{"created_on"=>"2013-10-23T00:58:15Z", "description"=>"azfazfazf zaf azffza", "homepage"=>"", "id"=>4, "identifier"=>"team-35", "name"=>"azfazf", "updated_on"=>"2013-10-23T00:58:15Z"}, "role_ids"=>["4"], "user_id"=>"12", "project_id"=>"4"}

尝试更改发送参数以将它们包装成membership. 文档中描述的类似内容

{
  "membership":
  {
    "user_id": 27,
    "role_ids": [ 2 ]
  }
}

我认为这应该有效

.post(:memberships,:membership => {:user_id => 12, :role_ids => [4]})
于 2013-10-25T07:08:12.173 回答