Rails - 重定向到显示传递参数的页面

时间:2015-05-17 10:18:09

标签: ruby-on-rails

我想从我的currencies控制器重定向到我的markets控制器,将当前货币作为参数(市场has_many种货币)传递。我想做这样的事情:

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

如何正确完成?

2 个答案:

答案 0 :(得分:1)

在config / routes.rb中定义路由

例如

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

在您的控制器中调用 redirect_to markets_show_url(id: @currency.market.id, currency: @currency.currency_name)

网址将

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

答案 1 :(得分:1)

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

markets的控制器中: -

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

作为市场has_many货币,当前currency currency_id应作为参数传递,以便在market控制器show操作中获取当前货币