Reputation: 1479
I'm new to rails and would like to have a question answered:
I have an upload csv field on my form which works perfectly fine when I actually upload a file. But when I hit the upload button without attaching a file, I get an exception: undefined method `[]' for nil:NilClass. Is there a way to stay on the same page and instead show a "no file uploaded" warning?
I've tried doing if(params[:model][:file].nil?) redirect_to model_path end
but it continues to the rest of the code. Thanks in advance for the help.
Views Code:
<%= form_for (@model), :url=>{ :controller=>"models", :action => 'create'}, :html=>{:multipart => true} do |f| %>
<p><%= f.label :file, 'Import csv file' %><br/>
<%= f.file_field :file, :accept => "text/csv"%></p>
<%= submit_tag "Upload" %>
<% end %>
Controller Code:
def create
51 @model = Model.new
52
53 @model.date_created = Time.now.utc
54
55
56 #handles csv input
59 if(params[:model][:file])
60
61 @model.id = SecureRandom.uuid;
62 file_data = params[:model][:file].read
63 @parsed_file = CSV.parse(file_data)
64 n=0
65 @parsed_file.each do |row|
74
75 if @model.save
76 n=n+1
77 end
79 end
81 else
82 @model.id = "different type"
83 end
84
85 respond_to do |format|
86 if @model.save
87 if(params[:model][:file] )
88 format.html { redirect_to @model, :notice => "Successfully imported the CSV file. #{n} new records added to the database" }
89 else
90 format.html { redirect_to @model, notice: 'Model entry was successfully created.' }
91 end
92 format.json { render json: @model, status: :created, location: @model }
93 else
94 format.html { render action: "new" }
95 format.json { render json: @model.errors, status: :unprocessable_entity }
96 end
Upvotes: 0
Views: 3758
Reputation: 396
To check if a file is attached for upload or not, you can simply check if a file exits in params with
if params[:file]
If you want to check for the extension of the file, you can with
File.extname(params[:file].path) == '.csv'
better way is to check for the content-type of the file and respond appropriately depending on the content-type you can do this with
params[:file].content_type == 'text/csv'
Upvotes: 0
Reputation: 6542
You should use Jquery validation.
For example :
<form name="myform" id="myform" action="#" method="post">
<input type="file" name="myfile" id="myfile" />
<input type="submit" name="submit" value="Submit" />
</form>
then you can validate the file input field when it is changed using the following code...
$(document).ready(function(){
$("#myform").submit({
var file_value = $('#myfile').val();
if ( file_value.length =< 0 ){
alert('No such file exist');
}
});
});
With in the same function, you can validate the format of file as well.
var ext = $('#myfile').val().match(/\.(.+)$/)[1];
switch (ext) {
case 'csv', 'CSV':
alert('CSV File- Correct File');
break;
default:
alert('This is not an allowed file type.');
}
Upvotes: 0
Reputation: 1442
try in controller...
if params[:model][:file].blank?
flash[:error] = 'Fields cannot be blank.'
redirect_to model_path
end
Second thing.. i wont recommend u to use 'model' as a controller bcoz it is rails keyword. Use another name.
Upvotes: 1