Uploading a File on aWebsite using Ruby Rails 4 -
Uploading a File on aWebsite using Ruby Rails 4 -
i using rails 4 , have code.
i created controller , called upload (upload_controller.rb)
i set code in it:
class uploadcontroller < applicationcontroller def index render :file => 'app\views\upload\uploadfile.rhtml.erb' end def uploadfile post = datafile.save(params[:upload]) render :text => "file has been uploaded successfully" end end
my model called data_file.rb. code following:
class datafile < activerecord::base attr_accessor :upload def self.save(upload) name = upload['datafile'].original_filename directory = "public/data" # create file path path = file.join(directory, name) # write file file.open(path, "wb") { |f| f.write(upload['datafile'].read) } end end
my view file called uploadfile.html.erb. code following:
<h1>file upload</h1> <%= form_tag({:action => 'uploadfile'}, :multipart => true) %> <p><label for="upload_file">select file</label> <%= file_field 'upload', 'datafile' %></p> <%= submit_tag "upload" %> <% end %>
my main goal of having code user can upload file specified location. file type has not images doc, excel sheets, etc. 1 time write code , bundle install , rake db:migrate , go tools , run development. 1 time go firefox , type in localhost:3000 , page cannot show. (directs me yahoo search or whatever).
i don't know doing wrong here. suppose add together gem or alter line or something? have been stuck on issue days , want move on next part of webpage. please help me. give thanks you.
ruby-on-rails ruby
Comments
Post a Comment