The problem about humanize
(and probably titleize
too i'm not sure) is that it breaks the view if the string is nil
. and then you'll have to .try(:humanize)
instead, which is a big headache if you realized that you made that mistake way too late and you have to recheck every line of code at your views. What i'm looking for is a way i could humanize
and titleize
strings before storing them into the database. That way, even if i'm looking straight into my database i will see the strings humanize
d and titleize
d.
You can create a method in your model which will take the attributes of this, and will transform as you want and/or you need, you can "fire" this every time you save a record using it as a before_save
callback:
For instance, having a model User
which attributes name
and lastname
I'm using the name
and using titleize
on it:
class User < ApplicationRecord
before_save :titlelize_names
def titlelize_names
self.name = self.name.titleize
end
end
The params will contain the attributes as the user has typed them, but they will be saved according to what the model says:
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "user"=>{"name"=>"name", "lastname"=>"lastname"}, "commit"=>"Create User"}
(0.1ms) begin transaction
SQL (0.8ms) INSERT INTO "users" ("name", "lastname", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Name"], ["lastname", "lastname"]...]
Is the only one interaction you need to do before save the record, the view is untouched and the controller just receive it and makes what model says.
If by any change it occurs some problem at the moment of saving the record you can use throw(:abort)
and "to force to halt" the callback chain.
def titlelize_names
self.name = self.name.titleize
throw(:abort)
end
In addition and depending on which method you're using with the before_save
callback, you can add custom errors which will be available to use within your controllers, as for instance, an error identified by the key :titleize_names
could be then handled in the controller.
def titlelize_names
errors.add(:titleize_names, 'An error has ocurred with the name attribute.')
self.name = self.name.titleize
throw(:abort)
end
You can use model callbacks to transform the data before saving it to the DB: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html - you probably want before_save
.
Also, if you want to ensure the record is never nil, add a validation as well.
Example:
class Example < ActiveRecord::Base
validates :name, presence: true
before_save :fixname
# Force titleize on :name attribute
def fixname
self.name = self.name.titleize
end
end
Now your model will have a method called fixname
that you can call anywhere on your model instance as well as having it called before saving it to the DB.
如果humanize
和titlerize
如果值是零返回例外,if语句创建一个,你可以使用一个服务或一个函数模型,使这个过程before_save
或before_create
回调:
value.titleize unless value.nil?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.