1

In a view I have links with the following format:

<%= link_to "User", move_on_path(organization: organization, url: edit_profile_path(@user)) %>

This uses a controller method that first calls on a method that sets a session value and then moves on to the specified url:

def move_on
  @organization = Organization.find_by(params[:organization])
  set_session(@organization)
  redirect_to params[:url]
end

This works but now I have an url for which the method is post rather then get. I don't know the syntax for the link when it's a post method. So I have the syntax below but need it to post to edit_profile_path. How can I specify this in the syntax below (or is there no way around changing def move_on for this)?

<%= link_to "User", move_on_path(organization: organization, url: edit_profile_path(@user)) %>

Update: Using @Chuck Callebs answer below I've changed the controller method to:

def move_on
  @organization = Organization.find_by(params[:organization])
  set_session(@organization)
  if params[:_url] == 'users#edit'
    post_to params[:url]
  else
    redirect_to params[:url]
end

Would this be correct? I made up post_to to explain what I would like it to do, but what would be the correct code as to make it post to the url?

I don't think it's a duplicate post since it's more about looking for the solution in this use case then about whether a post request is possible.

Nick
  • 3,496
  • 7
  • 42
  • 96
  • Why are you using POST in the first place? – kjmagic13 Aug 13 '15 at 16:03
  • For the above example of editing a user profile it indeed makes no sense, but I also have links for for example removing a role the user has in an organization. Thats where I use post requests. – Nick Aug 13 '15 at 17:32

2 Answers2

2

Unfortunately, I don't believe there's a way to use a redirect_to with a POST.

This is because redirect_to simply sends a HTTP 30x redirect header to the browser. The browser then initiates a GET request on the target URL. You have 2 options as I see them:

  • Make a new controller method for this particular redirect (a GET request, which would perform a similar task)

  • You can utilize request.get? method in your existing controller method to handle any discrepancies between the two requests.

Update

I think you should wrap the functionality in the POST request into a service object and just call it directly in the move_on method. That's the route I'd go. That being said, if you need to do the above approach, see this StackOverflow answer:

https://stackoverflow.com/a/10614765/14877

If you end up going that route, you should write a helper method (maybe even call it post_to :)) that would handle the details for you.

Community
  • 1
  • 1
Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
-1

following the link_to documentation http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

I think all you need is to add the method option:

<%= link_to "User", move_on_path(organization: organization, url: edit_profile_path(organization.id), method: :post) %>
thaleshcv
  • 752
  • 3
  • 7
  • This would cause the original click to be sent to move_on_path via a POST request, I believe what the OP is requesting is that the redirect is sent via POST which as Chuck pointed out is not possible. – Thomas Walpole Aug 13 '15 at 16:06
  • you're right @TomWalpole. Now I understand what he needs... – thaleshcv Aug 13 '15 at 16:09