2010-11-24 37 views
1

J'ai un formulaire pour créer un nouveau modèle de contact. Je saisis les valeurs à la main en coupant et en collant. Parfois, je finis par ajouter des espaces blancs sur la gauche et la droite.existe-t-il un moyen DRY d'utiliser strip sur tous les: params lors de la création d'un nouveau modèle dans Rails?

Voici ce qui est dans le contrôleur de création (j'ai une boucle qui vérifie si j'ai téléchargé une vcard qui, évidemment, ne présente généralement pas le problème (bien qu'il pourrait) - mais mon gros problème est quand je saisissez moi-même

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 

Répondre