0

I have a Kendo grid that is being binded to a remote data source.

I would like to insert a link to a google search when values in a specific column is Null.

<%= company_URL || link_to("Search", "https://www.google.com/search?q=#{company.name}%20leadership%20team") %>

I don't need to store this link in the database, so its more a presentation concern. I tried to put this in the JS using the template attribute, but I'm unclear how to write this as a function that would look like this:

 columns: [ {
   field: "name",
   template: function(dataItem) {
  return "<strong>" + kendo.htmlEncode(dataItem.name) + "</strong>";
  }

Or is it better to put this in the controllers or serializer since links are a presentation concern? I'm using active_model_serializer

Amal Dev
  • 1,938
  • 1
  • 14
  • 26
Mayer
  • 23
  • 5

1 Answers1

0

I think we might need a little bit more information... in your first code snippet you are showing normal Rails view syntax, you mention your attempt at using JS, but then you say you are using active model serializers. Those are very different use cases and it's hard to answer the question without knowing exactly what you need.

Either way, what you are asking is quite easy to do in a serializer. Assuming that you have a CompanySerializer, you might create a custom method in the serializer called company_link, like this..

class CompanySerializer < ActiveModel::Serializer

  attributes :id, :name, :company_link

  def company_link
    object.company_url ? object.company_url : "https://www.google.com/search?q=#{object.company.name}"
  end

end

In a serializer you can create as many custom methods as you want and then pass them into the attributes list. So, I used the generic method company_link as an in-between for company_url and your google search for the company name. Inside a serializer you automatically get access to object which is the object that is passed in, in this case, it is a company. I then used a ternary for an if else. If there is a company_url, use that, if not, output the google search link.

Eli Duke
  • 454
  • 1
  • 3
  • 14