Changing urls in ruby on rails depending on different conditions -



Changing urls in ruby on rails depending on different conditions -

i'm new ruby on rails....i wanted know if there way alter url displayed depending on client's response. mean... here's example: i'm making project showing listings in various places...

now in general have home page, search page, , detail page listings. so, respective urls officespace/home, officespace/search?conditions, officespace/detailpage?id=(controller-officespace)[&conditions eg.---price,size,place,type...]

so, every time client makes request search, same url shown, of course of study given conditions.

now want if client asks place , mentions nil size, price, etc., url should /listing/location_name.

if mentions other conditions, it'll listing/(office_type)/size(x sq feet)_office_for_rent_in_locationname)

b.t.w. (i have controller named listings , purpose else.)

and on ........... actually, want alter urls number of things. anyway, please help me. , please don't refer me manuals. i've read them , didn't give direct help.

this interesting routing challenge. essentially, goal create special look match kinds of url's want display in user's browser. these expressions used in match formulas in config/routes.rb. then, you'll need create sure form actions , links on relevant search pages link specialized url's , not default pages. here's illustration started:

routes.rb match "/listing/:officetype/size/:squarefeet/office_for/:saleorrent/in/:locationname" => "searches#index" match "/listing/*locationname" => "searches#index" resources :searches

since explicitly mentioned listings controller else, named our new controller searches. within code index method controller, have decide how want collect relevant info pass along view. marked : in match expressions above passed controller in params hash if http get query string parameter. can following:

searches_controller.rb def index if params[:squarefeet] && params[:officetype] && params[:locationname] @listings = listing.where("squarefeet >= ?", params[:squarefeet].to_i). where(:officetype => params[:officetype], :locationname => params[:locationname]) elsif params[:locationname] @listings = listing.where(:locationname => params[:locationname]) else @listings = listing.all end end

and send user 1 of links:

views/searches/index.html.erb <%= link_to "click here great office!", "/listing/corporate/size/3200/office_for/rent/in/dallas" %>

the above illustration work if listing model set same way arbitrary guess, can work there figure out code needs like. note wasn't able underscores in there. routes match segments separated slashes far can tell. maintain working on , may find way past that.

ruby-on-rails url routes

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

How do you set up a perforce server to work over the internet? -

ios - Lagging ScrollView with UIWebview inside -