views:

1180

answers:

2
    redirect_to :controller=>'groups',:action=>'invite'

but I got error because redirect_to send GET method I want to change this method to 'POST' there is no :method option in redirect_to what will I do ? Can I do this without redirect_to.

Edit:

I have this in groups/invite.html.erb

<%= link_to "Send invite", group_members_path(:group_member=>{:user_id=>friendship.friend.id,  :group_id=>@group.id,:sender_id=>current_user.id,:status=>"requested"}), :method => :post %>

This link call create action in group_members controller,and after create action performed I want to show groups/invite.html.erb with group_id(I mean after click 'send invite' group_members will be created and then the current page will be shown) like this:

redirect_to :controller=>'groups',:action=>'invite',:group_id=>@group_member.group_id

After redirect_to request this with GET method, it calls show action in group and take invite as id and give this error

Couldn't find Group with ID=invite

My invite action in group

    def invite
@friendships = current_user.friendships.find(:all,:conditions=>"status='accepted'")
@requested_friendships=current_user.requested_friendships.find(:all,:conditions=>"status='accepted'")
@group=Group.find(params[:group_id])
 end

The solution is I have to redirect this with POST method but I couldn't find a way.

Ugly solution: I solved this problem which I don't prefer. I still wait if you have solution in fair way.

My solution is add route for invite to get rid of 'Couldn't find Group with ID=invite' error.

in routes.rb

   map.connect "/invite",:controller=>'groups',:action=>'invite'

in create action

   redirect_to "/invite?group_id=#{@group_member.group_id}"

I call this solution in may language 'amele yontemi' in english 'manual worker method' (I think).

+1  A: 

The answer is that you cannot do a POST using a redirect_to.

This is because what redirect_to does is just send an HTTP 30x redirect header to the browser which in turn GETs the destination URL, and browsers do only GETs on redirects

LucaM
Yeah I got this but is there any way to solve my problem with redirect_to or without ?
+3  A: 

It sounds like you are getting tripped up by how Rails routing works. This code:

redirect_to :controller=>'groups',:action=>'invite',:group_id=>@group_member.group_id

creates a URL that looks something like /groups/invite?group_id=1.

Without the mapping in your routes.rb, the Rails router maps this to the show action, not invite. The invite part of the URL is mapped to params[:id] and when it tries to find that record in the database, it fails and you get the message you found.

If you are using RESTful routes, you already have a map.resources line that looks like this:

map.resources :groups

You need to add a custom action for invite:

map.resources :groups, :member => { :invite => :get }

Then change your reference to params[:group_id] in the #invite method to use just params[:id].

Steve Madsen
Thanks very much