Search code examples
ruby-on-railsrubylink-to

Ruby - link_to - How to add data directly from DB


First of all, I am very new to ruby and I am trying to maintain an application already running in production.

I have been so far able to "interpret" the code well, but there is one thing I am stuck at.

I have a haml.html file where I am trying to display links from DB.

Imagine a DB structure like below

link_name - Home
URL - /home.html
class - clear
id - homeId

I display a link on the page as below

< a href="/home.html" class="clear" id="home" > Home </a>

To do this I use 'link_to' where I am adding code as follows

-link_to model.link_name , model.url, {:class => model.class ...... }

Now I have a new requirement where we have a free text in DB, something like - data-help="home-help" data-redirect="home-redirect" which needs to come into the options.

So code in haml needs to directly display content versus assign it to a variable to display.

In other words I am able to do attr= '"data-help="home-help" data-redirect="home-redirect"' inside the <a>, but not able to do data-help="home-help" data-redirect="home-redirect" in <a> tag.

Any help would be greatly appreciated!


Solution

  • link_to accepts a hash :data => { :foo => "bar" } of key/val pairs that it will build into data- attributes on the anchor tag. The above will create an attr as follows data-foo="bar"

    So you could write a method on the model to grab self.data_fields (or whatever it's called) and split it into attr pairs and then create a hash from that. Then you can just pass the hash directly to the :data param in link_to by :data => model.custom_data_fields_hash

    This somewhat verbose method splits things out and returns a hash that'd contain: {"help"=>"home-help", "redirect"=>"home-redirect"}

    def custom_data_fields_hash
    
      # this would be replaced by  self.your_models_attr
      data_fields = 'data-help="home-help" data-redirect="home-redirect"'
    
      # split the full string by spaces into attr pairs
      field_pairs = data_fields.split " "
    
      results = {}
    
      field_pairs.each do |field_pair|
    
        # split the attr and value by the =
        data_attr, data_value = field_pair.split "="
    
        # remove the 'data-' substring because the link_to will add that in automatically for :data fields
        data_attr.gsub! "data-", ""
    
        # Strip the quotes, the helper will add those
        data_value.gsub! '"', ""
    
        # add the processed pair to the results
        results[data_attr] = data_value
    
      end
    
      results
    
    end
    

    Running this in a Rails console gives:

    2.1.2 :065 > helper.link_to "Some Link", "http://foo.com/", :data => custom_data_fields_hash
     => "<a data-help=\"home-help\" data-redirect=\"home-redirect\" href=\"http://foo.com/\">Some Link</a>"
    

    Alternatively you could make it a helper and just pass in the model.data_attr instead

    link_to "Some Link", "http://foo.com/", :data => custom_data_fields_hash(model.data_fields_attr)