Turning URL varables into URLs

34 views
Skip to first unread message

Ben Edwards

unread,
Oct 15, 2017, 9:39:07 AM10/15/17
to Ruby on Rails: Talk
OK, so I have a homepage and want the user to be able to select a link that goes back to it with a varable set.  Example here is the listings site lists all events regardless of price and I want users to be able to list events under £5.  So I have a link https://eventpuddle.com?price=cheap.  This is fine but for SEO reasons I want to have the URL something like https://eventpuddle.com/cheapevents.  Obviesly I could create another view but there are all sorts of things I will ultimatly want ot do this for.  https://eventpuddle.com/jazz, https://eventpuddle.com/pucn, https://eventpuddle.com/art...  i would end up with LOTS of views and have to do code changes for eatch (see below).

I came across something years ago (I think in PHP)  where URLs got unpacked into varables in the background.  So https://eventpuddle.com/price/cheapevents was mapped in the background and created a varable price with a value cheapevents.

This will also be usefull for listing events for a certain venue, whitch I have lots.  Dont want to create a view for eatch venue as there are lots and I dont want to have to code stuff if I add venues, I simply add a row in the database for the new venue.  The kind of URLs I would be looking for is https://eventpuddle.com/venue/old-market-assembly or https://eventpuddle.com/venue/canteen.

Wondering if there is a way of doing this in rails or another way of handeling the same type of thing.


 

Hassan Schroeder

unread,
Oct 15, 2017, 10:25:35 AM10/15/17
to rubyonrails-talk
On Sun, Oct 15, 2017 at 6:39 AM, Ben Edwards <lo...@funkytwig.com> wrote:

> I came across something years ago (I think in PHP) where URLs got unpacked
> into varables in the background. So
> https://eventpuddle.com/price/cheapevents was mapped in the background and
> created a varable price with a value cheapevents.

At the top of config/routes.rb --

# For details on the DSL available within this file, see
http://guides.rubyonrails.org/routing.html

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Ben Edwards

unread,
Oct 15, 2017, 1:49:10 PM10/15/17
to Ruby on Rails: Talk
Great, thanks, almost there.  Mine is slightly different as its not based on CRUD, its a ruby view based on a SQL view.  So

get '/patients/:id', to: 'patients#show'
 

Is not quite what I need.  If I have this view as root view (http://domain.tld) and want to pass cost as a variable (http://domain.tld?cost=cheapevents = http://domain.tld/cheapevents). Its the site root

currently I have

root 'upcoming_events#index'

get
'upcoming_events/index'

I tried

get 'upcoming_events/index:cost', to: 'upcoming_events#index'

And it works for root but if I try http://domain.tld/cheapevents I get

No route matches [GET] "/cheapevents"





Ben Edwards

unread,
Oct 15, 2017, 1:50:08 PM10/15/17
to Ruby on Rails: Talk
PS I also tried

get 'upcoming_events/cost', to: 'upcoming_events#index'

Ben Edwards

unread,
Oct 15, 2017, 1:54:52 PM10/15/17
to Ruby on Rails: Talk

Hassan Schroeder

unread,
Oct 15, 2017, 3:29:37 PM10/15/17
to rubyonrails-talk
On Sun, Oct 15, 2017 at 10:49 AM, Ben Edwards <lo...@funkytwig.com> wrote:
> Great, thanks, almost there. Mine is slightly different as its not based on
> CRUD, its a ruby view based on a SQL view.

Irrelevant. You can use any parameter and controller method names
you choose.

Aboobacker M K

unread,
Oct 16, 2017, 4:07:51 AM10/16/17
to rubyonra...@googlegroups.com
You can use something like this

get 'venue/:name:', to: 'venues#show'

class VenueController
  def show
    # render the view by checking params[:name]
  end
end

Check the above linked routing guide for more details

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCwEmH12Lc0yJqhuUR75P3chQMUMR%3DXRDb01Wmf_0h_8Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Regards
Aboobacker MK

Mugurel Chirica

unread,
Oct 31, 2017, 6:04:48 AM10/31/17
to rubyonra...@googlegroups.com
You can try something like this if the requirements match:

  match "/*path", to: redirect { |params, request|
    # Add your logic here or delegate the work to a specific class

    # Proof of concept
    slug = 'jazz' # You get the slug from the params
    price_category = true # You deduce if the slug matches any of your tags
 
    if price_category
      "/#{slug}?price=cheap"
    else
      slug
    end

  }, via: :all

kopf1988

unread,
Jan 24, 2018, 9:22:56 PM1/24/18
to Ruby on Rails: Talk
I don't know why you're still trying /cheapevents when you haven't defined it at all.

Not best practices really, but try something like:

get 'events/:cost', to: 'upcoming_events#index'

and then in your controller, respond based on things like:

if params[:cost] == "cheap"
  @events = Events.where("price < 5")
end

So you can then navigate to url.name/events/cheap
Reply all
Reply to author
Forward
0 new messages