在 Rails 中创建新模型时,是否有一种 DRY 方法可以在所有 :params 上使用 strip ?
问题描述:
我有一个用于创建新联系人模型的表单.
I have a form to create a new Contact model.
我通过剪切和粘贴手动输入值.
I enter the values by hand by cutting and pasting.
有时我最终会在左侧和右侧添加空白.
Sometimes I end up adding white space on the left and right.
这是创建控制器中的内容(我有一个循环来检查我是否上传了一个 vcard,显然,这通常不会出现问题(尽管它可能会出现)——但我的大问题是当我输入时自己做.
Here is what is in the create controller (I have a loop that checks if I have uploaded a vcard which, obviously, doesn't typically present the problem (although it could) -- but my big problem is when I type it myself.
def create
@contact = Contact.create(params[:contact])
unless @contact.vcard.path.blank?
paperclip_vcard = File.new(@contact.vcard.path)
@vcard = Vpim::Vcard.decode(paperclip_vcard).first
@contact.title = @vcard.title
@contact.email = @vcard.email
@contact.first_name = @vcard.name.given
@contact.last_name = @vcard.name.family
@contact.phone = @vcard.telephone
@contact.address.street1 = @vcard.address.street
@contact.address.city = @vcard.address.locality
@contact.address.state = @vcard.address.region
@contact.address.zip = @vcard.address.postalcode
@contact.company_name = @vcard.org.fetch(0)
end
@contact.user_id = current_user.id # makes sure every new user is assigned an ID
if @contact.save
#check if need to update company with contact info
@contact.update_company
@contact.new_todos #create the todos for the newly created contact
flash[:notice] = "Successfully created contact."
redirect_to @contact
else
render :action => 'new'
end
end