0

What helper can I use in model to populate html code as string in view?

This is the method I currently use in my model:

 def state_country_name
    "#{self.name}  (#{self.country.name})"
 end

I want to wrapp {self.country.name} inside a span, with class: pull-right.

I have already tried:

  def state_country_name
    "#{self.name} #{helpers.content_tag(:span, self.country.name, class: "pull-right")}"
  end

  def helpers
    ActionController::Base.helpers
  end

Result:

London <span>England</span>

I use the autocomplete-rails4-gem and this is my form input:

= f.input :city_name, :url => autocomplete_city_name_companies_path, :as => :autocomplete, :id_element => "#company_city_id", input_html: {:value => @company.city.name, class: 'form-control'}

Code for my autocomplete_city_name_companies action:

autocomplete :city, :name, :full => false, :display_value => :state_country_name, :extra_data => [:state_id]
16
  • 2
    Why are you doing this in the model? Commented Jan 18, 2016 at 15:45
  • Yes, as mentioned, in the view the <span> is in html code instead of string in view. Commented Jan 18, 2016 at 15:46
  • 1
    Then you should do it in the frontend as well. Never generate HTML in the model Commented Jan 18, 2016 at 15:48
  • 1
    @Rubioli this is a good use case for a presenter - draper is fairly popular. This will allow you to use the autocomplete gem, and add that method in a clean way. Commented Jan 18, 2016 at 16:02
  • 1
    @Rubioli, presenters don't have to be big and bloated, this is not a big thing to write and the benefits of introducing the pattern are long-lived. I've written some sample code below to demonstrate this. Commented Jan 18, 2016 at 16:22

2 Answers 2

2

I'd recommend taking the Presenter approach here, as it'll give you a place to put presentation logic that can work with the ruby models but whose logic does not belong in the model itself. You can use Draper or one of several other gems that do this, but here is some code to demonstrate how simple this concept really is:

A sample presenter:

class CompanyPresenter < Struct.new(:company)
  def state_country_name
    company.country.name
  end

  # act as proxy for unknown methods
  def method_missing(method, *args, &block)
    company.public_send(method, *args, &block)
  end
end

@presentable_company = CompanyPresenter.new(@company)

Or if you want to take the decorator approach:

module CompanyPresenter
  def state_country_name
    country.name
  end
end

class Company < ActiveRecord::Base
  def decorate!
    self.extend CompanyPresenter
  end
end

@company.decorate!
Sign up to request clarification or add additional context in comments.

Comments

1

I think you shouldn't do it in your models. Instead put your helpers methods in your helpers file. In your model_helper.rb:

def my_helper(model)
html = <<-EOT
<span class="pull-right">{model.country.name}</span>
EOT
html.html_safe
end

In your view:

<%= my_helper(@model_object) %>

1 Comment

Just beware of using html_safe for anything that may not actually be 'safe' (primarily data from an unknown source).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.