7

I am very new to ROR. I have a task to finish:

Here's the Model:

class File::DataImport < ActiveRecord::Base
  attr_accessible :created_by, :file_name, :file_source, :updated_at, :updated_by
end

Here's the Controller:

class Files::DataImportsController < ApplicationController
  def index
  end

  def new
  end
end

And the views I have are index and new.

I want a field to upload data. The data should be stored in the server and save the filepath into the database in a specified column file_name. The path should be default to all uploading files.

I am stuck with how to start. Please help me to find the solution and I will learn from this.

Thanks in advance.

3 Answers 3

9

db/migrate/20110711000004_create_files.rb

class CreateFiles < ActiveRecord::Migration
def change
  create_table :files do |t|
  t.string :name
  # If using MySQL, blobs default to 64k, so we have to give
  # an explicit size to extend them
  t.binary :data, :limit => 1.megabyte
  end
end
end

app/controllers/upload_controller.rb

 class UploadController < ApplicationController
 def get
 @file = File.new
 end
 end

app/views/upload/get.html.erb

<% form_for(:file,
url: {action: 'save'},
html: {multipart: true}) do |form| %>
Upload your file: <%= form.file_field("uploaded_file") %><br/>
<%= submit_tag("Upload file") %>
<% end %>

app/models/file.rb

class File < ActiveRecord::Base
def uploaded_file=(file_field)
self.name = base_part_of(file_field.original_filename)
self.data = file_field.read
end
def base_part_of(file_name)
File.basename(file_name).gsub(/[^\w._-]/, '')
end
end

app/controllers/upload_controller.rb

def save
@file = File.new(params[:file])
if @file.save
redirect_to(action: 'show', id: @file.id)
else
render(action: :get)
end
end

app/controllers/upload_controller.rb

def file
@file = File.find(params[:id])
send_data(@File.data,
filename: @File.name,
disposition: "inline")
end

app/controllers/upload_controller.rb

def show
@file = File.find(params[:id])
end

app/views/upload/show.html.erb

<h3><%= @file.name %></h3>
<img src="<%= url_for(:action => 'file', :id => @file.id) %>"/>
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried your solution 9year later and I have this error: Unpermitted parameter: :uploaded_file if you are still alive could you help me?
1

you should consider using one of the already available solutions like paperclip: https://github.com/thoughtbot/paperclip or carrierwave: https://github.com/jnicklas/carrierwave

Besides the Readmes there are also good tutorials out there:

http://railscasts.com/episodes/134-paperclip

http://railscasts.com/episodes/253-carrierwave-file-uploads

edit: As you want to implement it yourself I recommend examining the sources of the above on Github and try to understand what their code is doing. Also I would not bother implementing it myself, but if you have your reasons this might get you going..

1 Comment

Hello frank, Thanks for your reply. But I should not use any gem or plugins for this. Thanks
0

You might want to look into a solution such as carrierwave.

The Github page provides a good explanation on how to use it, but this is also a nice guide.

3 Comments

Hi Zajn, Thanks and the problem is that i should not use any gem or any plugin's.
What is restricting you from using a gem? It's probably better to not try and reinvent the wheel here, unless you have a really good reason to do so.
This is like a Test. So I should learn from this.... That's why i should not use any gem or any plugin's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.