On 21 June 2011 19:14, Jen <jen.bot...@gmail.com> wrote:
> Hi,
> I have been experimenting with the code from the replies to this post so
> far, but still can't achieve what I want. If a file is selected it is
> uploaded just fine. However if no file is selected the application spits out
> the following error:
> NoMethodError in UploadController#create
>
> undefined method `original_filename' for nil:NilClass
>
> Rails.root: /home/resource_portal/website
>
> Application Trace | Framework Trace | Full Trace
>
> rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in
> `method_missing'
> app/models/upload.rb:13:in `save'
> app/controllers/upload_controller.rb:13:in `create'

You said that you have taken note of the previous replies, but looking
at the code in upload_controller.rb you have not tested
params[:upload] before calling Upload.save in the create action, which
was the point of your original question I think.  As I previously
suggested you need to use something like
if !params[:upload].blank?
to prevent save being called when params[:upload] is nil or empty.
What else you need to do in that action when it is empty I do not know
as I do not know the details of what you want to achieve.

Colin

> rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/implicit_render.rb:4:in
> `send_action'
> rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/abstract_controller/base.rb:151:in
> `process_action'
> rake-0.8.7/ruby/1.9.1/gems/actionpack-3.0.3/lib/action_controller/metal/rendering.rb:11:in
> `process_action'
>
> ...
>
> I'm guessing I therefore need to be able to validate whether there is
> something in 'file_field' before the post action happens, but I'm not sure
> how to do this. Any other suggestions would be great!
>
> Also Now I'm confused about the best place for this code to go. I was
> originally just putting one if statement in my controller, but as this is
> validation I guess it should go in the model.
>
> I should probably also point out that files are not being stored in a
> database. Instead I am using GIT to version them and keep track of their
> history.
> My current code is attached.
>
> Thanks,
> Jen.
>
>
> On 21/06/11 13:46, Chirag Singhal wrote:
>
> It would be better to delegate that job to Upload model.
> Then you could write something like this:
> #posts to the data model.
> post = Upload.new(params[:upload])
> if post.save
>   puts "file uploaded"
>   render 'upload'
>   repo.commit_all(params[:message])
> else
>   redirect_to 'upload'
> end
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/74m04USkgCsJ.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to