rails对字符串是否有“人性化”的对立面?

时间:2021-10-16 11:40:22

Rails adds a humanize() method for strings that works as follows (from the Rails RDoc):

Rails为如下(来自Rails RDoc)的字符串添加了humanize()方法:

"employee_salary".humanize # => "Employee salary"
"author_id".humanize       # => "Author"

I want to go the other way. I have "pretty" input from a user that I want to 'de-humanize' for writing to a model's attribute:

我想走另一条路。我有来自一个用户的“漂亮的”输入,我想把它“去人性化”,用于编写模型的属性:

"Employee salary"       # => employee_salary
"Some Title: Sub-title" # => some_title_sub_title

Does rails include any help for this?

rails对此有什么帮助吗?

Update

In the meantime, I added the following to app/controllers/application_controller.rb:

同时,我在app/controller /application_controller.rb中添加了以下内容:

class String
  def dehumanize
    self.downcase.squish.gsub( /\s/, '_' )
  end
end

Is there a better place to put it?

有更好的地方放置它吗?

Solution

Thanks, fd, for the link. I've implemented the solution recommended there. In my config/initializers/infections.rb, I added the following at the end:

谢谢fd的链接。我已经实现了那里推荐的解决方案。在我的配置初始化/感染。rb,我在结尾添加了以下内容:

module ActiveSupport::Inflector
  # does the opposite of humanize ... mostly.
  # Basically does a space-substituting .underscore
  def dehumanize(the_string)
    result = the_string.to_s.dup
    result.downcase.gsub(/ +/,'_')
  end
end

class String
  def dehumanize
    ActiveSupport::Inflector.dehumanize(self)
  end
end

3 个解决方案

#1


139  

the string.parameterize.underscore will give you the same result

string.parameterize。下划线将提供相同的结果

"Employee salary".parameterize.underscore       # => employee_salary
"Some Title: Sub-title".parameterize.underscore # => some_title_sub_title

or you can also use which is slightly more succinct (thanks @danielricecodes).

或者你也可以使用更简洁的(感谢@danielricecodes)。

"Employee salary".parameterize("_")       # => employee_salary

#2


3  

There doesn't appear to be any such method in the Rail API. However, I did find this blog post that offers a (partial) solution: http://rubyglasses.blogspot.com/2009/04/dehumanizing-rails.html

在Rail API中似乎没有任何此类方法。然而,我发现了这篇提供(部分)解决方案的博文:http://rubyglasses.blogspot.com/2009/04/dehumanizing-rails.html

#3


1  

In http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html you have some methods used to prettify and un-prettify strings.

在http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html中,您有一些用于修饰和取消修饰字符串的方法。

#1


139  

the string.parameterize.underscore will give you the same result

string.parameterize。下划线将提供相同的结果

"Employee salary".parameterize.underscore       # => employee_salary
"Some Title: Sub-title".parameterize.underscore # => some_title_sub_title

or you can also use which is slightly more succinct (thanks @danielricecodes).

或者你也可以使用更简洁的(感谢@danielricecodes)。

"Employee salary".parameterize("_")       # => employee_salary

#2


3  

There doesn't appear to be any such method in the Rail API. However, I did find this blog post that offers a (partial) solution: http://rubyglasses.blogspot.com/2009/04/dehumanizing-rails.html

在Rail API中似乎没有任何此类方法。然而,我发现了这篇提供(部分)解决方案的博文:http://rubyglasses.blogspot.com/2009/04/dehumanizing-rails.html

#3


1  

In http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html you have some methods used to prettify and un-prettify strings.

在http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html中,您有一些用于修饰和取消修饰字符串的方法。