在 Rails 中创建新模型时,是否有一种 DRY 方法可以在所有 :params 上使用 strip ? [英] is there a DRY way to use strip on all :params when creating a new model in Rails?

查看:52
本文介绍了在 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

推荐答案

这可能会有所帮助:http://scottmoonen.com/2009/05/08/rails-pattern-trim-spaces-on-input/

这篇关于在 Rails 中创建新模型时,是否有一种 DRY 方法可以在所有 :params 上使用 strip ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆