logo       

[Rails] Re: File upload: msg#03297

RubyonRailsTalk

Subject: [Rails] Re: File upload

hum, the question is the file it's represented in a model?
anyway, i used virtual attributes...
any questions, just ask =)

at sfile.rb

validate_on_create :validate_upload

def temp_file= temp_file
   @temp_file = temp_file
end

protected

def validate_update
    errors.add_to_base("Upload a valid Flash file") unless validate_type_file @temp_file
end

def validate_type_file file
    original_filename = file.original_filename
    extension = SFile.ret_ext_file original_filename
    reg_file = Regexp.new(/(flw)/)
    if n =~ reg_file
      return true
    else
     return false
   end
end

def self.ret_ext_file n
    v = n.split('.')
    v = v[v.size-1].downcase
    return v
  end


at .erb 

<h1>Welcome</h1>

<% form_for(@sfile) do |f| %>
 <%= f.error_messages %>

 <p>
   <%= f.label :title %><br />
   <%= f.text_field :title %>
 </p>
 <p>
   <%= f.label :description %><br />
   <%= f.text_area :description %>
 </p>
  <p><%= file_field 'i', 'temp_file' %></p>
 <p>
   <%= f.submit 'Create' %>
 </p>
<% end %>

at sfile_controller.rb

def create
                @sfile = Sfile.new(params[:sfile])
                @sfile.attributes = params[:i]
                respond_to do |format|
                        if @sfile.save
                                flash[:notice] = 'File was successfully created.'
                                format.html { redirect_to(@sfile) }
                        else
                                format.html { render :action ="" "new" }
                        end
                end
end

2009/7/31 Gabriel Bianconi <bianconi.gabriel@xxxxxxxxx>

Hello. I'm new with Rails and I'm trying to create a file upload app.


I want to make that it stores the file in public/data, that only .swf
files can be uploaded and that the filename in my server is the same
from the form (see below).

I currently have these files and want to integrate the upload to that
form, but I don't know how to do that.

Thank you,
Gabriel.

---------------

class SfilesController < ApplicationController
        def new
                @sfile = Sfile.new
        end

        def create
                @sfile = Sfile.new(params[:sfile])
                respond_to do |format|
                        if @sfile.save
                                flash[:notice] = 'File was successfully created.'
                                format.html { redirect_to(@sfile) }
                        else
                                format.html { render :action ="" "new" }
                        end
                end
        end

        def show
                @sfile = Sfile.find(params[:id])
        end
end

--------------

class Sfile < ActiveRecord::Base
        validates_presence_of :title
end

----------------

<h1>Welcome</h1>

<% form_for(@sfile) do |f| %>
 <%= f.error_messages %>

 <p>
   <%= f.label :title %><br />
   <%= f.text_field :title %>
 </p>
 <p>
   <%= f.label :description %><br />
   <%= f.text_area :description %>
 </p>
 <p>
   <%= f.submit 'Create' %>
 </p>
<% end %>



--~--~---------~--~----~------------~-------~--~----~
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@xxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@xxxxxxxxxxxxxxxx
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | Mail Home | sitemap | FAQ | advertise