Treating URL/string in Ruby on Rails

Asked

Viewed 199 times

0

I have to edit an incoming URL, but in some cases the URL may contain an external reference with http and it breaks my system, because it only accepted paths to its own platform.

Follow a code snippet and some outputs:

<%-object.each do |o| -%>
<%- url = @context.nil? ? polymorphic_path([ :adm, o]) : polymorphic_path([ :adm, @context, o])-%>

Some exits:

<%= o -%> = #<Collection:0x7f068b067870>

<%= @context.nil? -%> = true

<%= polymorphic_path([ :adm, o]) -%> = /admin/pagina/biblioteca

Here’s my mistake, sometimes this link can be external, and when concatenates I can’t make it work:

<%= polymorphic_path([ :adm, o]) -%> = /admin/pagina/https://livraria.com

I believe that this return of the variable url is a String.

  • You can inspect the value of the variable "the" ?

  • I have already solved, the variable "url" has a string as return and ruby accepts regular expressions. soon post the Regex used.

1 answer

1

I saw that you already found a solution, but if anyone has similar doubt, just replace http or https in the link:

link.gsub(/https?\/\//, '')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.