On 6 December 2012 13:43, Jean-David <[email protected]> wrote:
>
>
> Le jeudi 6 décembre 2012 13:23:58 UTC+1, Colin Law a écrit :
>>
>> On 6 December 2012 12:11, Jean-David <[email protected]> wrote:
>> > ...
>> > Thanks for the advise. Logging led me to a solution, but I don't
>> > really understand what's going on under the hood.
>> >
>> > Here's my solution:
>> >
>> > "upload" now returns this:
>> >   return "#{uploaded_io.original_filename}"
>> >
>> > In "create", I set:
>> >   @lead.illustration = upload
>> >
>> > In "update", this wont work. I need to set:
>> >   params[:lead][:illustration] = upload
>> > and the update goes fine.
>> >
>> > After finding this out I tried to set
>> >   params[:lead][:illustration] = upload
>> > instead of
>> > @lead.illustration = upload
>> > in "create", and that wont work.
>>
>> Can you post your create and update methods as you now have them so I
>> can see what you are doing.
>
>
> Sure, here goes :
>
>   def create
>     @lead = Lead.new( params[:lead] )
>
>     @lead.illustration = upload_image
>
>     if @lead.save
>       flash[:success] = "Leaderboard successfully added."
>       redirect_to @lead
>     else
>       redirect_to root_path
>     end
>   end
>
>
>   def update
>     @lead = Lead.find_by_id( params[:id] )
>
>     params[:lead][:illustration] = upload_image

You said that doing @lead.illustration = upload_image (presumably at
this point) did not work.  It should do, provided that
params[:lead][:illustration] is not present.  If it is then the value
from upload_image will be overwritten by the value from params when
you call  update_attributes.  Another possibility is that if the
illustration is the only field being changed then update_attributes
may not actually save the record (as it does not think anything has
changed).

Probably leaving it as you have it is the simplest solution.

Colin

>
>     if @lead.update_attributes( params[:lead] )
>       flash[:success] = "Leaderboard successfully updated."
>       redirect_to @lead
>     else
>       render action: "edit"
>     end
>   end
>
>
>   def upload_image
>     if params[:lead][:illustration]
>       uploaded_io = params[:lead][:illustration]
>
>       File.open(Rails.root.join('app', 'assets', 'images',
> uploaded_io.original_filename), 'wb') do |file|
>         file.write(uploaded_io.read)
>       end
>
>       return "#{uploaded_io.original_filename}"
>     end
>   end
>
>
>
>>
>> Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to