Reputation: 435
I have created a custom action req
inside my membership controller and defined routes for it.But when i give path to that custom action on link_to ,it gives me error saying that unknown action ,The action 'show' could not be found for MembershipsController.I dny understand why it goes to find show action when i am giving path to my custom action.
Following is my custom action inside membership controller
def req
@user =User.find_by_email(params[:email])
@group =params[:group_id]
unless @group.nil?
if Membership.request(@user.id, @group)
redirect_to :back, :notice => 'Joined successfully.'
else
redirect_to :back, :notice => 'couldnot Joine.'
end
end
end
There is no show action inside my memberships controller.
Following is the route for my custom action :
resources :memberships do
collection do
post 'req'
end
end
Following is the code inside my groups/show.html.haml
where i am giving path of custom action..
- if @collegemates.empty?
%p.info You have no common collegemates ...
- else
%table.datatable
%thead
%tr
...
%tbody
- @collegemates.each do |c|
- unless Membership.group_member(c.id, @group)
%tr
%td= c.full_name
%td= c.email
%td= link_to "request to join",{:controller => :memberships, :action => "req",:email =>c.email,:group_id => @group.id},:method => "post"
When i click on request to join I am getting that unknown action error for show action. What am i doing wrong here?
Upvotes: 2
Views: 2274
Reputation: 43318
To make a link do a POST request, you need javascript. Rails takes care of this for you, but you do have to include the necessary files. Adding <%= javascript_include_tag :defaults %>
to your layouts file will solve your problem. If you don't do this a GET request will be performed instead of a POST request.
Upvotes: 2
Reputation: 6728
As default rails gives the CRUD actions.
new,create,edit,update,delete
the code
collection do
post 'req'
end
will create the route as below.
/memberships/req
So rails misunderstands that you are requesting /memberships/:id and tries to match the show action.as the there is no show action it raises unknown action.
So the solution is you need to inform that you are not using CRUD actions
It can be done as below
resources :memberships, :only => [] do
collection do
post 'req'
end
end
Upvotes: 0
Reputation: 10423
You may want to assign your action either to collection or to member.
resources :memberships do
post 'req', :on => :collection # or :member
end
See http://guides.rubyonrails.org/routing.html#adding-more-restful-actions.
:on => :collection
creates a route like memberships/req
where
:on => :member
gives you a route for an object like memberships/:id/req
.
Upvotes: 1