bsky
bsky

Reputation: 20222

Rails - redirect to show page passing a parameter

I want to redirect from my currencies controller to my markets controller, passing the current currency as a parameter(the market has_many currencies). I want to do something like this:

  redirect_to markets_show_url(id: @currency.market.id, currency: @currency)

How can this be done correctly?

Upvotes: 1

Views: 530

Answers (2)

Shamsul Haque
Shamsul Haque

Reputation: 2451

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.

Upvotes: 1

djaszczurowski
djaszczurowski

Reputation: 4515

define route in config/routes.rb

for example

get "markets/:id" => "markets#show", as: :markets_show

invoke in your controller redirect_to markets_show_url(id: @currency.market.id, currency: @currency.currency_name)

Url will be

http://example.com/markets/your_id?currency=YOUR_CURRENCY

Upvotes: 1

Related Questions