Topic: I am Unable to Validate the file field "validates_presence_of :photo"

Hi...
I am Kranthi. I am working on Rails. Your Example is very good. But, for me the Validate code is not working.
validates_presence_of :location, :message => "Missing required field location"
acts_as_taggable
I am getting an error for "acts_as_taggable"
Please.... Help me....
MY CODE IS LIKE THIS:
================
MODEL:
======
class User < ActiveRecord::Base
  validates_presence_of :firstname, :lastname, :username, :role, :message => ": Should not be null."
  acts_as_taggable #this is reporting an Error for me
  def upload(params)
        path = get_path(params)
        File.open(path, "wb") { |f| f.write(params[:photo].read) }
  end
  def get_path(params)
      if params[:photo]
        name = params[:photo].original_filename
        directory = FileUtils.mkdir_p("public/users/"+username)
       path = File.join(directory, name)
        return path
      else
        return "error"
      end
  end
  def get_url(params)
      name = params[:photo].original_filename
      directory = "data"
      path = File.join(directory, name)
      puts name
      return name
  end
end


CONTROLLER:
==========
class UsersController < ApplicationController
  # POST /users
  # POST /users.xml
  def create
    @user = User.new(params[:user])
    respond_to do |format|
      if @user.upload(params[:user])
        @user.photo = @user.get_url(params[:user])
        puts @user.photo
      if @user.save
        flash[:notice] = 'User was successfully created.'
        format.html { redirect_to(@user) }
        format.xml  { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
    end
  end
end

VIEW:
=====
<h1>New user</h1>
<% form_for(@user, :html => {:multipart => "true"}) do |f|  %>
  <%= f.error_messages %>
  <p>
    <%= f.label :firstname %><br />
    <%= f.text_field :firstname %>
  </p>
  <p>
    <%= f.label :lastname %><br />
    <%= f.text_field :lastname %>
  </p>
  <p>
    <%= f.label :username %><br />
    <%= f.text_field :username %>
  </p>
  <p>
    <%= f.label :photo %><br />
    <%= f.file_field :photo %>
  </p>
  <p>
    <%= f.label :role %><br />
    <%= f.text_field :role %>
  </p>
  <p>
    <%= f.label :inuse %><br />
    <%= f.check_box :inuse %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>
<%= link_to 'Back', users_path %>

MY PROBLEM:
==========
I am unable to perform validations for :photo field. Whether it is empty or not... I want to validate: vlidate_presence_of and validates_format_of and validate_filesize_of and all these things.....
Please Give me a Suggestion How to get validations.... for File Upload field :photo


And one more thing... I am using Google Chrome.. it is not providing me the full path of that file.

Re: I am Unable to Validate the file field "validates_presence_of :photo"

Did you install the plugin?

If so, then you likely need to restart your server.

Re: I am Unable to Validate the file field "validates_presence_of :photo"

Nice to hear from you....

But, i have installed all the listed plugins.

The problem is my form validating other fields like: firstname, lastname, username ..... But, it is excluding only the :photo column(file_field) only....

Re: I am Unable to Validate the file field "validates_presence_of :photo"

I am trying to understand your problem - are you getting an actual error?
If so, can you post the full backtrace?
Does everything work without the validations in place?

kranthipoturaju wrote:

I am Kranthi. I am working on Rails. Your Example is very good. But, for me the Validate code is not working.
validates_presence_of :location, :message => "Missing required field location"

Where does location come into play?

Re: I am Unable to Validate the file field "validates_presence_of :photo"

All the validations are working properly for :username, :firstname and all.....
But, it is not validating the presence for the "file_field" in my code it is :photo.

U can see... I kept "validations_presnence_of" for :photo field also.
But is going in to the upload(params) function and there it is returning the Error like this:
"You have a nil object when you didn't expect it!
The error occurred while evaluating nil.original_filename"

Means null pointer Exception is occured....

Why it is not validating that field only.

Re: I am Unable to Validate the file field "validates_presence_of :photo"

Add some loggers and make sure you are passing the right information around.
For example:

def create
    logger.debug "params passed to create ##############"
    logger.debug params.inspect
    @user = User.new(params[:user])
    logger.debug "What does @user contain at this point???????????????"
    logger.debug @user.inspect
    respond_to do |format|
      if @user.upload(params[:user])
        @user.photo = @user.get_url(params[:user])
....

Do you really need to pass params around like that?  You already set @user.photo with this line:

@user = User.new(params[:user])