代码之家  ›  专栏  ›  技术社区  ›  Satchel

在Rails中创建新模型时,是否有一种在所有参数上都使用strip的干燥方法?

  •  1
  • Satchel  · 技术社区  · 15 年前

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

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

    有时我会在左右两侧添加空白。

    这里是创建控制器中的内容(我有一个循环,检查我是否上传了一个vCard,很明显,它通常不会出现问题(尽管它可以),但我的大问题是当我自己键入它时。

     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
    
    1 回复  |  直到 15 年前