mercredi 7 octobre 2015

I want to update file to an existing rails record with carrierwave

I have a form where i am asking a user to upload a file.This file to be uploaded is supposed to be saved to a existing record. When a user upload this file i don't want it to be saved right away(because it will create a new record), i want to access the parameters in the controller and update image attribute to the existing record.Currently when i access the params and update_attribute, it shows like it did update but when i look in the database , image attribute is still nil. It works fine when i save directly as new record but it won't let me update manually,can somebody help me? Thank you in advance

Form

<%= form_tag url_for(:controller=>"deal",:action=>"create_deal"),:method=>'get',:html=>{:multipart=>true}  do  %>
       <%= file_field_tag :image, :value=> params[:image] %>

       <%= submit_tag "upload" %>

       <%end%>

Controller

def create_deal
    @attachment = params[:image]
    e_record = Deal.find(:last) #get existing record
    if e_record.update_attribute(:image, @attachment)
       redirect_to :action=>"profile"
    end
end

Aucun commentaire:

Enregistrer un commentaire