I'm trying to user Redmine via REST API in order to keep and update Projects Membership, as explained in http://www.redmine.org/projects/redmine/wiki/Rest_Memberships
In order to do so, I included, in my client application, written in RoR, the following 'redmine_project' model:
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
Then , in my console, I'm trying to include a new membership, with the following command:
RedmineProject.find(4).post(:memberships,:user_id => 12, :role_ids => [4])
The first part of the commend is properly treated by Redmine (actually, if I just try RedmineProject.find(4) I do get a JSON with my project data) but.. I'm unable to Post the new membership with
.post(:memberships,:user_id => 12, :role_ids => [4])
Here is the result of my application console:
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'
and here is the result of the redmine terminal:
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'
Is there something wrong with my code? or is it a (unlikely) bug?
I do assumption that you use Redmine v2.3
Looking at at the place where error is raised I see that members
is empty array!
Looking at the process of building members
I can suppose that params[:membership]
is blank, please see your log:
{"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"}
Try to change sending parameters to wrap them into membership
.
Something like this described in the document
{
"membership":
{
"user_id": 27,
"role_ids": [ 2 ]
}
}
I think this should work
.post(:memberships,:membership => {:user_id => 12, :role_ids => [4]})