I’m new to Rails. I’m developing a store builder.
What I want
I want a root level url for each shop.
http://greatsite.com/my-shop-name
My Solution
shop_controller.rb
def show
if params[:url]
@shop_ref = params[:url]
@shop = Shop.where(:url => @shop_ref).first
else
@shop_ref = params[:id]
@shop = Shop.find(@shop_ref)
redirect_to "/" + @shop.url
return
end
if @shop.nil?
render 'show_invalid_shop', :object => @shop_ref and return
end
render 'show' => @shop
end
def create
@shop_url = (0...8).map{65.+(rand(25)).chr}.join.downcase
@shop = Shop.new(:url => @shop_url)
if @shop.save
redirect_to "/" + @shop.url
else
render :action => "new"
end
end
routes.rb
...
resources :shops
match ':url' => 'shops#show', :constraints => { :url => /[a-z|0-9]{4,30}/ }
...
The Problem
Crap Performance. (It’s ugly as sin too, of course.)
Every time someone creates a new shop (which is one click from our home page), it creates a new shop and does a redirect. In New Relic, I see this is killing performance – a lot of time is spent in “Request Queuing”.
Is there any neater and faster way of achieving what I want?
I’m not sure why the redirects would be causing such a headache, but:
Could you do something like:
Might be useful to look at: http://pjax.heroku.com/
It’s not exactly pretty, but if redirects are really that bad it might help?