redirect_to using POST in rails
Redirection isn't possible with POST requests - it's part of the HTTP/1.1 protocol.
You could either introduce another step that contains the form data to be POSTed to the payment processor, or you could send the post from your application (something I have done when working with PROTX).
Rails redirect_to post method?
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]
.
Redirect_to with post params
I'll quote the beginning of this SO answer for later viewers:
You can't do a redirect and send POST data at the same time in the
HTTP spec.
Redirect user to external url with post method and params
You cannot redirect a user to a POST
method. You can present a user with a link that the Rails JS will turn into a form/POST:
= link_to "Do it!", "http://other.site/path", method: :post
...or a form that triggers a POST
to another site (note that I've included an extra param in this) eg.
= form_tag "http://other.site/path", method: :post do
= hidden_field_tag :foo, "Bar"
= submit_tag "Do it!"
...or you can make the request yourself from the server side on your user's behalf (here I'm using the httparty gem), note that I'm including the same foo=Bar
parameters:
class YourController < ApplicationController
include HTTParty
def some_action
self.class.post "http://other.site/path", query: { foo: "Bar" }
end
end
A few options for you.
Can I redirect_to using the `PUT` verb in Rails 4.2
The standard does not allow for this. A redirect with status 307 forbids the browser from changing the request method when following the redirect but there is no mechanism for requesting a particular method be used.
Ruby on Rails POST parameters on redirect_to
You can't do a redirect and send POST data at the same time in the HTTP spec. Redirects are implemented by sending a simple Location: otherlocation.html
header. POST data doesn't fit anywhere into that system.
Do you want the user to go to this page, or do you want to just send the data to the application yourself? If you want to send the data and not send the user there, use Ruby's Net::HTTP module. If you want to send the user, you may be forced to output a view with a form, and submit it automatically with Javascript. (Don't forget to degrade gracefully by offering a submit button in noscript
tags.)
how can I use redirect_to :controller = posts, :action = index in views?
Why would you want to redirect in your view ? The redirection has to be done in the controller.
If you want to make a link to posts#index use this <%= link_to "Posts", posts_path %>
Related Topics
Restarting a Loop from the Top
How to Understand Nil Vs. Empty Vs. Blank in Ruby
How to Pick Randomly from an Array
What Exactly Is the Singleton Class in Ruby
Rhc Setup Gives Error 'No Such File Dl/Import'
Ruby'S File.Open and the Need For F.Close
Issue Updating Ruby on MAC With Xcode 4.3.1
Find Out If Current Time Is Between Two Times
Difference Between "Or" and || in Ruby
Combining Implicit Wait and Explicit Wait Together Results in Unexpected Wait Times
How to Avoid Running Activerecord Callbacks