Ruby on rails 在模型中使用helper
我可以在模型中使用什么帮助程序在视图中将html代码填充为字符串 这是我当前在模型中使用的方法: 我想将Ruby on rails 在模型中使用helper,ruby-on-rails,ruby,model,helper,Ruby On Rails,Ruby,Model,Helper,我可以在模型中使用什么帮助程序在视图中将html代码填充为字符串 这是我当前在模型中使用的方法: 我想将{self.country.name}包装在span中,使用class:pull right 我已经试过: 结果: London <span>England</span> 我的autocomplete\u city\u name\u companys的代码操作: autocomplete :city, :name, :full => false, :displa
{self.country.name}
包装在span中,使用class:pull right
我已经试过:
结果:
London <span>England</span>
我的autocomplete\u city\u name\u companys的代码
操作:
autocomplete :city, :name, :full => false, :display_value => :state_country_name, :extra_data => [:state_id]
我认为你不应该在你的模特身上这样做。而是将helpers方法放在helpers文件中。 在模型_helper.rb中:
def my_helper(model)
html = <<-EOT
<span class="pull-right">{model.country.name}</span>
EOT
html.html_safe
end
def my_helper(模型)
html=我认为你不应该在你的模型中这样做。而是将helpers方法放在helpers文件中。
在模型_helper.rb中:
def my_helper(model)
html = <<-EOT
<span class="pull-right">{model.country.name}</span>
EOT
html.html_safe
end
def my_helper(模型)
html=我建议在这里采用Presenter方法,因为它将为您提供一个放置表示逻辑的位置,该逻辑可以与ruby模型一起工作,但其逻辑不属于模型本身。您可以使用Draper或其他几种gems中的一种来实现这一点,但以下是一些代码来演示这个概念到底有多简单:
示例演示者:
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)
class CompanyPresenter
或者,如果您想采用decorator方法:
module CompanyPresenter
def state_country_name
country.name
end
end
class Company < ActiveRecord::Base
def decorate!
self.extend CompanyPresenter
end
end
@company.decorate!
module CompanyPresenter
def state_country_name
country.name
结束
结束
类公司
我建议在这里采用Presenter方法,因为它将为您提供一个放置表示逻辑的位置,该逻辑可以与ruby模型一起工作,但其逻辑不属于模型本身。您可以使用Draper或其他几种gems中的一种来实现这一点,但以下是一些代码来演示这个概念到底有多简单:
示例演示者:
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)
class CompanyPresenter
或者,如果您想采用decorator方法:
module CompanyPresenter
def state_country_name
country.name
end
end
class Company < ActiveRecord::Base
def decorate!
self.extend CompanyPresenter
end
end
@company.decorate!
module CompanyPresenter
def state_country_name
country.name
结束
结束
类公司
为什么要在模型中执行此操作?是的,如前所述,在视图中是html代码,而不是视图中的字符串。然后,您也应该在前端执行此操作。永远不要在应用程序中生成HTMLmodel@Rubioli这是演示者的一个很好的用例-draper相当流行。这将允许您使用自动完成gem,并以一种干净的方式添加该方法。@Rubioli,演示者不必过于庞大和臃肿,这不是一件大事,引入该模式的好处是长期存在的。我在下面编写了一些示例代码来演示这一点。为什么要在模型中执行此操作?是的,如前所述,在视图中是html代码而不是视图中的字符串。然后,您也应该在前端执行此操作。永远不要在应用程序中生成HTMLmodel@Rubioli这是演示者的一个很好的用例-draper相当流行。这将允许您使用自动完成gem,并以一种干净的方式添加该方法。@Rubioli,演示者不必过于庞大和臃肿,这不是一件大事,引入该模式的好处是长期存在的。我在下面编写了一些示例代码来演示这一点。对于任何可能不“安全”(主要是来自未知源的数据)的内容,请小心使用html\u-safe
。对于任何可能不“安全”(主要是来自未知源的数据)的内容,请小心使用html\u-safe
。
module CompanyPresenter
def state_country_name
country.name
end
end
class Company < ActiveRecord::Base
def decorate!
self.extend CompanyPresenter
end
end
@company.decorate!