Tag: 干燥

Rails 3在所有表单上删除before_validation的空格

我对Rails相对较新,有点惊讶这不是一个可配置的行为……至少没有一个我能找到的?!? 我原以为99%的表单会受益于从所有string和text字段中修剪的空白?!? 猜猜我错了…… 无论如何,我正在寻找一种干燥的方法来从Rails 3应用程序中的表单字段(类型:string&:text)中删除所有空格。 视图有自动引用(包含?)并可用于每个视图的助手……但模型似乎没有这样的东西?!? 或者他们呢? 所以目前我做的是首先 要求的 , 然后 包括 whitespace_helper(又名WhitespaceHelper)。 但这对我来说似乎仍然不是很干,但它有效…… ClassName.rb: require ‘whitespace_helper’ class ClassName < ActiveRecord::Base include WhitespaceHelper before_validation :strip_blanks … protected def strip_blanks self.attributeA.strip! self.attributeB.strip! … end LIB / whitespace_helper.rb: module WhitespaceHelper def strip_whitespace self.attributes.each_pair do |key, value| self[key] = value.strip if value.respond_to?(‘strip’) end end 我想我正在寻找一个单一的(DRY)方法(类?)来放置一个( lib/ ?),它将获取一个params(或属性)列表并从每个属性w中删除空格( .strip! […]

在Rails中使用partials的最佳实践

为了与DRY原则保持一致,一旦我重复特定模式超过一次或两次,我会尝试使用partials。 因此,我的一些观点包含十个或更多不同的部分。 我担心这可能会对整体表现产生负面影响。 一些编程书籍将部分的使用与方法的使用进行了比较。 那么我应该使用相同的理由来确定何时使用它们? 关于Rails项目中部分的大小和数量的最佳实践是什么?

rails – DRY respond_to重复操作

在我的一个rails控制器中,我必须响应几种类型的格式,所以我使用典型的respond_to链: respond_to do |format| format.html { … } format.mobile { … } format.jpg { … } format.xml { … } format.js { … } end 通常, { … }部分会以多种格式重复出现。 在这种情况下保持DRY的最佳方法是什么? 在html , mobile和xml有“重复”动作的场景中,我想做类似这样的事情: respond_to do |format| format[:html, :mobile, :xml] { … } format.jpg { … } format.js { … } end 非常感谢。

干掉一个帮手:包装form_for并访问本地表单变量

我试图干掉一堆表单代码,这些表单代码在每个表单的末尾出现一组重复的字段。 我写了一个包装form_for rails helper的帮助器。 但是我开始迷失在飞来飞去的所有不同范围…… 我的助手是这样的: def simple_form_helper(record_or_name_or_array, *args, &proc) options = … # overriding some options, not relevant form_for(record_or_name_or_array, *(args < “blah”)) , &proc) # i wish to access &proc and append the call to render # to within &procs scope (to access block local variable) concat render(‘shared/forms/submit’) # this obv does not work […]

DRYING rails视图:partial vs helper

我需要有关DRYing视图代码的最佳实践的建议。 我的应用程序中有三个类(NewsItem,RssItem和BlogItem),它们使用不同的视图,但在它们中有相似的部分。 其中一个部分是这样的: 这三个class级几乎相同,所以我决定把它带到一个单独的地方。 在这里我很困惑:我应该使用部分或辅助方法吗? 我知道帮助程序主要用于从HTML中分离ruby代码,但在这种情况下,帮助程序将如下所示: def toolbar_for(item, type_str, edit_path) if current_user content_tag(:footer) do |b| marks_bar(item).to_s << (delete_from_favorite_button(item, type_str) || favorite_button(@item, type_str)).to_s << share_button(@item).to_s << (content_tag(:div) { link_to("Edit", edit_path)} if current_user.is_mine?(@item)).to_s end end end 所以,这里几乎没有HTML代码。 你能不能给我建议,你认为哪种方法更好,为什么? 此外,这些方法中是否存在一些性能问题(例如,多个字符串串联或频繁的部分加载可能代价高昂)? (这个应用程序相当高负载)

如何编写跨越模型,控制器和视图的Rails mixin

为了减少我的小Rails应用程序中的代码重复,我一直在努力将我的模型之间的公共代码添加到它自己的独立模块中,到目前为止一直很好。 模型的东西相当简单,我只需要在开头包含模块,例如: class Iso < Sale include Shared::TracksSerialNumberExtension include Shared::OrderLines extend Shared::Filtered include Sendable::Model validates_presence_of :customer validates_associated :lines owned_by :customer def initialize( params = nil ) super self.created_at ||= Time.now.to_date end def after_initialize end order_lines :despatched # tracks_serial_numbers :items sendable :customer def created_at=( date ) write_attribute( :created_at, Chronic.parse( date ) ) end end 这工作正常,但是,现在,我将要有一些控制器和视图代码,这些代码在这些模型之间也是常见的,到目前为止,我有这个用于我的可发送内容: # […]