ShortUrl.get(params[:key]))
redirect_params = params.permit!.except("language", "special", "controller", "action", "key").to_h
path = URI.parse(short_url.value)
path.query = path.query + URI.encode_www_form(redirect_params)
short_url.view!
redirect_to path.to_s
else
redirect_to root_url
end
end
Не совсем по делу, но я бы поменял index на show ещё :) мне кажется это семантически логичнее. Ещё я бы проверил не теряется ли там "&"
Поправил так: def index if params[:key].present? && (short_url = ShortUrl.get(params[:key])) redirect_params = params.permit!.except("language", "special", "controller", "action", "key").to_h path = URI.parse(short_url.value) path.query = [path.query, URI.encode_www_form(redirect_params)].compact.join('&') short_url.view! redirect_to path.to_s else redirect_to root_url end end
def index short_url = short_url_by_key(params[:key]) return redirect_to(root_url) if short_url.nil? path = generate_path(short_url) short_url.view! redirect_to path end def generate_path(short_url) path = URI.parse(short_url.value) path.query = [path.query, URI.encode_www_form(redirect_params)].compact.join('&') path.to_s end def short_url_by_key(key) return if key.nil? ShortUrl.get(key) end def redirect_params params.permit!.except("language", "special", "controller", "action", "key").to_h end
Обсуждают сегодня