Passing Param Values to Redirect_To as Querystring in Rails

Passing param values to redirect_to as querystring in rails

The 'Record' form of redirect_to uses the second argument only for the response status. You'll have to use another form of redirect_to, like the 'String' form. e.g.:

redirect_to thing_path(@thing, :foo => params[:foo])

which will work for nested params[:foo] params like you mentioned. Or, as Drew commented below, you can use polymorphic_url (or _path):

redirect_to polymorphic_path(@thing, :foo => params[:foo])

How to properly pass parameter in query string with redirect_to? (Rails 4)

You should not build urls manually by string concatenation, because some characters need to be encoded in valid urls. If possible use url helper that take parameters.

I do not know if this works (depends on the implementation of contactable):

@contactable.redirect_url(email: @contact.email)

If that is not possible ensure valid encoding yourself:

"#{@contactable.redirect_url}?email=#{ERB::Util.url_encode(@contact.email)}"

Rails routes redirect for query string

The question mention by Matt helped me to figure out my answer (thanks a lot!). It was a slightly different for my specific case. Im leaving the answer that worked for me for future reference.

match "/old/report/:client" => redirect{ |params, request| "/new/report/#{params[:client]}?#{request.query_string}" }

How do I do a redirection in routes.rb passing on the query string

You can use request object in this case:

match "/invoices" => redirect{ |p, request| "/dashboard?#{request.query_string}" }

ruby - redirect_to(url, :myparam = 'abc')

From the documentation:

redirect_to(options = {}, response_status = {})

Redirects the browser to the target specified in options. This
parameter can take one of three forms:

Hash - The URL will be generated by calling url_for with the options.

Record - The URL will be generated by calling url_for with the
options, which will reference a named URL for that record.

String starting with protocol:// (like http://) or a protocol relative
reference (like //) - Is passed straight through as the target for
redirection.

You're passing a String as the first argument, so it's using the 3rd option. Your second parameter is interpreted as the value for the response_status parameter.

So, if your redirect is an internal one (to the same app), you don't need to specify the scheme and hostname. Just use

redirect_to root_url(param1 => 'abc', param2 => 'xyz')

If it's an external URL, build the complete URL before redirecting:

url = "www.mysite.com/?param1=abc¶ms2=xyz"
redirect_to url

Adding url parameters with redirect_to

Yes, just pass your query string parameters in to contact_path:

redirect_to contact_path(@contact, :show_family => 'yes')

How can I selectively add query parameters in redirect_to?

First just compose a hash containing the parameters you want - for example:

opts = session.slice(:sort, :ratings)
.merge(params.slice(:sort, :ratings))
.compact_blank

This example would contain the keys :sort, :ratings with the same keys from the parameters merged on top (taking priority).

You can then pass the hash to the desired path helper:

redirect_to foos_path(**opts)

You can either just pass a trailing hash option or use the params option to explitly set the query string:

irb(main):007:0> app.root_path(**{ sort: 'backwards' })
=> "/?sort=backwards"
irb(main):008:0> app.root_path(params: { ratings: 'XX' })
=> "/?ratings=XX"
irb(main):009:0> app.root_path(params: { })
=> "/"

An empty hash will be ignored.

If your calling redirect_to with a hash instead of a string you can add query string parameters with the params: key:

redirect_to { action: :foo, params: opts }

If you're working with an arbitrary given URL/path and want to manipulate the query string parameters you can use the URI module together with the utilities provided by Rack and ActiveSupport for converting query strings to hashes and vice versa:

uri = URI.parse('/foo?bar=1&baz=2&boo=3')
parsed_query = Rack::Utils.parse_nested_query(uri.query)
uri.query = parsed_query.except("baz").merge(x: 5).to_query
puts uri.to_s # => "/foo?bar=1&boo=3&x=5"

Rails - redirect to show page passing a parameter

redirect_to market_url(@currency.market, currency_id: @currency.id)

In controller of markets:-

def show
@market = Market.find(params[:id])
@currency = Currency.find(params[:currency_id])
end

As the market has_many currencies so, for current currency currency_id should be passed as argument to get the current currency in the market controller show action.



Related Topics



Leave a reply



Submit