Chiperific
Chiperific

Reputation: 4686

Rails 5 redirect_back with url query parameter option

According to the docs:

redirect_back(fallback_location:, allow_other_host: true, **args)

  • :fallback_location- The default fallback location that will be used on missing Referer header.

  • :allow_other_host - Allow or disallow redirection to the host that is different to the current host, defaults to true.

  • All other options that can be passed to redirect_to are accepted as options and the behavior is identical.

    And redirect_to allows me to add params to the url just by passing them as a hash

    So, why do none of these work for me:

  • redirect_back fallback_location: tasks_path, allow_other_host: false, syncing: true
  • redirect_back fallback_location: tasks_path, allow_other_host: false, { syncing: true }
  • redirect_back fallback_location: tasks_path, allow_other_host: false, options: { syncing: true }
  • redirect_back(fallback_location: tasks_path, allow_other_host: false, options: { syncing: true })
  • redirect_back(fallback_location: tasks_path, allow_other_host: false, syncing: true)
  • ...and any other iteration on the above that I could think of.

    All of them (that are valid code), just return me back without the added parameter

    I'm trying to achieve this URL: (back_url or fallback_location) + '?syncing=true'

    Upvotes: 4

    Views: 3906

    Answers (4)

    marsimus
    marsimus

    Reputation: 81

    You also just can add value to flash object e.g. flash[:my_value] = '123', and then access it in the next request

    Upvotes: 1

    wastetime909
    wastetime909

    Reputation: 1342

    I bumped into this same issue just now. While not being able to find a direct solution, what I did was create a session and store the parameters in the session. After being redirected, my controller can grab the parameters from the session then delete it. Not really a "Rails way", but get what I needed.

    Hope that helps.

    Upvotes: 2

    Chiperific
    Chiperific

    Reputation: 4686

    While I get what @Kkulikovskis is saying, I would argue that the docs are confusing as they suggest I can pass additional *args and have them respond as they would to redirect_to.

    So, I wrote a helper method:

    def redirect_back_for_sync
      if request.env['HTTP_REFERER'].present? &&
         request.env['HTTP_REFERER'] != request.env['REQUEST_URI']
        redirect_to request.env['HTTP_REFERER'] + '?syncing=true'
      else
        redirect_to properties_path(syncing: true)
      end
    end
    

    Now I can call redirect_back_for_sync in my controller instead of using redirect_back at all.

    Upvotes: 4

    Kkulikovskis
    Kkulikovskis

    Reputation: 2088

    If you look at the source code for redirect_back you will see, that it essentially uses redirect_to "whatever_url.com" version of redirect_to method.

    If you check the explanation of redirect_to you can see that in this use case you unfortunately cannot pass any arguments. If this is super needed, I guess you could just override the redirect_back method to append params option to the url with string concatenation, but that seems like a nasty fix.

    But to answer your question - what you want to achieve seems to be impossible out of the box.

    Upvotes: 5

    Related Questions