Ruby replace html fragments with specified links - ruby-on-rails

I have a Rails Model with field "description" and other Model Person with field "title".
I want to add ability to replace all Person titles into description field to correct links
For example John Smith to <a href='/person/1'>John Smith</a>
Also I want to avoid tag dupliation inside itself.
Of course person name could be an anchor of tag in phrase, for example <a href='any link'>Meet John Smith</a> - that case of course should stay same.
And I want to make that action as button, which could be pressed any times with correct same result - when all single non A-anchor texts will be replaced with links.
How could I reach it? With xpath or regexp maybe?

Related

Create links in Kentico reports

I'd like to add clickable links to a Kentico Report. The report editor allows you to add all kinds of HTML mark-up in the layout, but it doesn't allow you to add HTML INSIDE of a table that you've inserted into the layout. (Or if it does, it is not obvious from the UI, or from the Kentico documentation.) I want a link to appear in each row, and the link should include a value from that row.
Clicking any of the links would open another page that shows more data about a particular record. In my case, my first column is an ID column and I want its value (in each row) to behave like a hyperlink to another page whose URL includes the clicked ID value as a parameter.
We can use jquery within our Kentico report to allow each value in a particular field to cause a link to be opened when clicked. In my case, I have a URL into which I want to embed an ID value from the report. I want to open one of the admin pages whose URL looks like the following (where 9999 is replaced with a record ID from my report):
/CMSModules/AdminControls/Pages/UIPage.aspx?elementguid=00000000-0000-abcd-0123-000000000000&objectid=9999&displaytitle=false
So let's assume the first field in the report is an ID column and we want to make the displayed ID behave like a link to some other page.
First we need jquery. Edit the report's layout in '<>Source' mode and add a script reference for jquery, such as one you can get from code.jquery.com, or just reference it locally if you have it:
<script src="/jquery-3.4.0.min.js"></script>
Next, we must find each ID field and then make it behave like a hyperlink. To do so, we find the <th> with the ID column's title, walk up to the <table>, and then find all <tr>s immediately under the <tbody>. Once we have each <tr>, we iterate through them to:
underline the ID value like a hyperlink
set the cursor to a hand like a hyperlink
add an onclick event to do open a URL (in a new tab) with my ID field's value
So here is the script. Add it just like you added the jquery script tag. (but add it after the jquery script tag)
<script>
$('th:contains("MyIDColumnTitle")').parents('table').first().children('tbody').children('tr').each(function() {
$(this).children('td').first()
.css('text-decoration','underline')
.css('cursor','pointer')
.click(function(){
var thisId = $(this).text();
var u = "/CMSModules/AdminControls/Pages/UIPage.aspx?elementguid=00000000-0000-abcd-0123-000000000000&objectid=" + thisId + "&displaytitle=false"
window.open(u,'_blank');
});
});
</script>
Keep in mind that if MyIDColumnTitle is not very unique, this script may find the wrong th and table. Modify the jquery selector to suit your needs. You may want to add a wrapping element around your report that has an element ID so you can be specific with your selector.
It wouldn't be difficult to take the same concept and use it to launch a page in a modal dialog instead.

How to autolink all words in a rendered text that match an item name in my database?

I have a rails app with a postgres database for articles and items. I would like that whenever I render text from an article, it can somehow scan the text and if it finds a word that matches the name of an item in my database it autolinks to that items show_path.
Any ideas? Much appreciated.
You can reason it out from first principles.
Make a little method that returns either a word, or a link to an article if the word is in the article.
def make_link(word)
item = Item.find_by(text: word.downcase)
item ? link_to(word, item_path(item_id) : word
end
Now, map all the words in the article using the method above to convert the linkable ones into links.
new_article = ActionController::Base.helpers.sanitize(article).split(' ').map{|word| make_link(word)}.join(' ').html_safe
Note the html_safe method to ensure the link tags aren't escaped when the new_article is rendered, and the sanitize method to ensure unwanted HTML tags in the source are removed.

How do extract query strings from URL and save them in a database?

In an existing page (developed manually in early 90) I have over 1500 text list similar to the one below:
- Abenakis, Pancake Mix (Buckwheat), 1 kg, $4.32 Add to Cart
and its "Add to Cart" HTML:
<a href="http://ww6.paymentcompany.com/cf/add.cfm?userid=87378855
&product=Abenakis,+Pancake+Mix+(Buckwheat),+1+kg
&price=4.32
&scode=ABCD012
&return=www.mysite.com/food.html">Add to Cart</a>
The following query strings change with every "Add To Cart" <a> anchor:
&product=Abenakis,+Pancake+Mix+(Buckwheat),+1+kg
&price=4.32
&scode=ABCD012
I want to extract all elements in the URL and save them in DB table, then I can display them in a non-list way.
I thought of using Nokogiri, but based on what I have tried i didn't get it.
Is there a simple way to do that?
First you can parse your URL using URI.parse to separate out the query terms. Then you can create a hash from URI::decode_www_form and use the query terms however you like:
uri = URI.parse("http://ww6.paymentcompany.com/cf/add.cfm?userid=87378855&product=Abenakis,+Pancake+Mix+(Buckwheat),+1+kg&price=4.32&scode=ABCD012&return=www.mysite.com/food.html")
Hash[URI::decode_www_form(uri.query)]
# => {"userid"=>"87378855", "product"=>"Abenakis, Pancake Mix (Buckwheat), 1 kg", "price"=>"4.32", "scode"=>"ABCD012", "return"=>"www.mysite.com/food.html"}

Rails: many-to-one display on form via dynamically added input text boxes

I have a form in rails that allows the user to create a new object (call it a search).
This object has_many excluded_phrases.
What I would like is the ability to display one text box per excluded phrase added.
The form will start out with only a single text box, allowing the user to add one excluded phrase. If they want to add more, there will be button labeled "+" that will dynamically add one more text box, and allow the user to add as many more items as he wants.
On form submit, this should populate the db with the user search, and create all the required phrases that are linked to that user search.
Help?
All inspiration needed is there:
http://railscasts.com/episodes/197-nested-model-form-part-2
This popular rails plugin does exactly what you want.

naming form files using square brackets

I've noticed in some php applications, that form fields are labelled with [] in them.
Say a shopping cart page, that lists all the items where you can edit the quantity.
is the [] type naming used to get the correct row?
Just trying to figure out how I should name each textbox?
should it be like:
name="quantity-<%= items.RowId %>"
THen when I loop the form fields, I would get the index number?
This is done in PHP when you need to pass <select multiple="multiple"> to the server, otherwise PHP will catch only the first selected value.
Textboxes don't normally require such naming.
This might answer a few of your questions. Here, Phil Haack uses square brackets in order to bind to a model that contains a list.

Resources