domi771
domi771

Reputation: 450

How do I get a param which is a part of a URL?

I need to get the param out of a URL:

demo.com/market/usdeur

I need to define the market according to the value from the URL. I am able to do it with:

?market=usdeur

@market = Global[params[:market]]

but not when only having the URL available. What I am missing?

This is what I have so far:

 params do
    use :market
  end
  get "/:market" do
    @market =  params[:market]
  end

Upvotes: 0

Views: 48

Answers (1)

engineersmnky
engineersmnky

Reputation: 29308

You need to add a route for this since it looks like Sinatra and not Rails. Have you tried

get '/market/:market' do
  @market = params[:market]
end

In Rails it would be more like this in routes.rb

get '/market/:market', to: 'markets#show', as: :market_path

Or something similar.

Right now to access that route you would have to visit http://www.example.com/usdeur when what you want is http://www.example.com/market/usdeur

Update

If this is truly Rails, add the above route to routes.rb and then in the MarketController add this

def show
  #I used #find_by_name not exactly sure what attribute usdeur refers to so you might
  #need to change this
  @market = Market.find_by_name(params[:market]) 
end

Now @market will be an instance of Market given usdeur is the name.

Upvotes: 2

Related Questions