首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Rails中创建新模型时,有没有一种干式的方法来使用strip on all :params?

在Rails中创建新模型时,有没有一种干式的方法来使用strip on all :params?
EN

Stack Overflow用户
提问于 2010-11-25 06:15:41
回答 1查看 2.9K关注 0票数 1

我有一个表单来创建一个新的联系人模型。

我通过剪切和粘贴手动输入值。

有时我会在左边和右边添加空格。

下面是create控制器中的内容(我有一个循环来检查我是否上传了vcard,显然,它通常不会出现问题(尽管它可能会出现) --但我最大的问题是我自己输入的时候。

代码语言:javascript
复制
 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-25 07:05:40

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4272028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档