开发者

is there a DRY way to use strip on all :params when creating a new model in Rails?

开发者 https://www.devze.com 2023-01-27 00:48 出处:网络
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.

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.

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
       @contac开发者_运维技巧t.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


This might help: http://scottmoonen.com/2009/05/08/rails-pattern-trim-spaces-on-input/

0

精彩评论

暂无评论...
验证码 换一张
取 消