在模型中使用助手
Using helper in model
我可以在 model 中使用什么助手来填充 html code as string 在 查看 ?
这是我目前在模型中使用的方法:
def state_country_name
"#{self.name} (#{self.country.name})"
end
我想用 class: pull-right
.
在 span 中包装 {self.country.name}
我已经试过了:
def state_country_name
"#{self.name} #{helpers.content_tag(:span, self.country.name, class: "pull-right")}"
end
def helpers
ActionController::Base.helpers
end
结果:
London <span>England</span>
我使用 autocomplete-rails4-gem 这是我的表单输入:
= 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'}
我的 autocomplete_city_name_companies
操作代码:
autocomplete :city, :name, :full => false, :display_value => :state_country_name, :extra_data => [:state_id]
我认为你不应该在你的模型中这样做。而是将您的助手方法放在您的助手文件中。
在你的 model_helper.rb:
def my_helper(model)
html = <<-EOT
<span class="pull-right">{model.country.name}</span>
EOT
html.html_safe
end
在您看来:
<%= my_helper(@model_object) %>
我建议在此处采用 Presenter 方法,因为它将为您提供放置可与 ruby 模型一起使用但其逻辑不属于模型本身的表示逻辑的位置。您可以使用 Draper 或执行此操作的其他几个 gem 之一,但这里有一些代码可以证明这个概念实际上是多么简单:
示例演示者:
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)
或者如果您想采用装饰器方法:
module CompanyPresenter
def state_country_name
country.name
end
end
class Company < ActiveRecord::Base
def decorate!
self.extend CompanyPresenter
end
end
@company.decorate!
我可以在 model 中使用什么助手来填充 html code as string 在 查看 ?
这是我目前在模型中使用的方法:
def state_country_name
"#{self.name} (#{self.country.name})"
end
我想用 class: pull-right
.
{self.country.name}
我已经试过了:
def state_country_name
"#{self.name} #{helpers.content_tag(:span, self.country.name, class: "pull-right")}"
end
def helpers
ActionController::Base.helpers
end
结果:
London <span>England</span>
我使用 autocomplete-rails4-gem 这是我的表单输入:
= 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'}
我的 autocomplete_city_name_companies
操作代码:
autocomplete :city, :name, :full => false, :display_value => :state_country_name, :extra_data => [:state_id]
我认为你不应该在你的模型中这样做。而是将您的助手方法放在您的助手文件中。 在你的 model_helper.rb:
def my_helper(model)
html = <<-EOT
<span class="pull-right">{model.country.name}</span>
EOT
html.html_safe
end
在您看来:
<%= my_helper(@model_object) %>
我建议在此处采用 Presenter 方法,因为它将为您提供放置可与 ruby 模型一起使用但其逻辑不属于模型本身的表示逻辑的位置。您可以使用 Draper 或执行此操作的其他几个 gem 之一,但这里有一些代码可以证明这个概念实际上是多么简单:
示例演示者:
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)
或者如果您想采用装饰器方法:
module CompanyPresenter
def state_country_name
country.name
end
end
class Company < ActiveRecord::Base
def decorate!
self.extend CompanyPresenter
end
end
@company.decorate!