Ruby on Rails
Thursday, December 6, 2012
NOOB ALERT
Now I warned you, here's the problem that's been getting me mad for hours
I've got a model for content I'm calling "lead". It has a title, a text and an image used as a background.
I'm not using paperclip or CarrierWave to handle the image file (and I don't want to, one of my needs here is to learn how this all works).
I'm using this function to upload the file (it comes directly from the rails guides) :
def upload
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
@lead.illustration = "#{uploaded_io.original_filename}"
end
end
When creating a "lead", everything goes fine with create:
def create
@lead = Lead.new( params[:lead] )
upload
if @lead.save
flash[:success] = "Lead successfully added."
redirect_to @lead
else
redirect_to root_path
end
end
The "lead" gets saved, I can use it. So far, so good.
Now with the problem (remember the NOOB ALERT).
Updating a lead without modifying the image goes fine, but updating with a new image returns this error:
NoMethodError in LeadsController#update
undefined method `name' for nil:NilClass
and
app/controllers/leads_controller.rb:45:in `update'Here's my update function:
def update
@lead = Lead.find_by_id( params[:id] )
upload
if @lead.update_attributes!(params[:lead])
flash[:success] = "Leaderboard successfully updated."
redirect_to @lead
else
render action: "edit"
end
end
Basically the same as create. I've been googling and going back and forth for hours and I didn't get to solve this. Any help would be more than welcome.
-- 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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/t6V4GCC_EoMJ.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment