Reputation:
I know how to do this with match
but I really want to do it in the resource block. Here's what I have (simplified):
resources :stories do
member do
'put' collaborator
'delete' collaborator
end
end
I am looking for a way to allow the same action name in the URL but have different entry points in the controller. At the moment I've got in my controller:
# PUT /stories/1/collaborator.json
def add_collaborator
...
end
# DELETE /stories/1/collaborator.json
def remove_collaborator
...
end
So I tried this:
resources :stories do
member do
'put' collaborator, :action => 'add_collaborator'
'delete' collaborator, :action => 'remove_collaborator'
end
end
but this didn't seem to work when I wrote an rspec unit test:
describe "PUT /stories/1/collaborator.json" do
it "adds a collaborator to the story" do
story = FactoryGirl.create :story
collaborator = FactoryGirl.create :user
xhr :put, :collaborator, :id => story.id, :collaborator_id => collaborator.id
# shoulds go here...
end
end
I get this error:
Finished in 0.23594 seconds
5 examples, 1 failure, 1 pending
Failed examples:
rspec ./spec/controllers/stories_controller_spec.rb:78 # StoriesController PUT
/stories/1/collaborator.json adds a collaborator to the story
I'm assuming this error is because the way I'm trying to define my routes is incorrect...any suggestions?
Upvotes: 3
Views: 3339
Reputation: 1865
Is the following better?
resources :stories do
member do
put 'collaborator' => 'controller_name#add_collaborator'
delete 'collaborator' => 'controller_name#remove_collaborator'
end
end
You should also check your routes by launching in a terminal:
$ rake routes > routes.txt
And opening the generated routes.txt file to see what routes are generated from your routes.rb file.
Upvotes: 2