Ruby on Rails Tuesday, February 1, 2011

OK, I must first inform you, I'm no rails guru (not yet anyways) but here is what I think should be your directory structure:

app/
     controllers/
                uploads_controller.rb  # This will contain the UploadController class
     views/
            uploads/
                  index.html.erb   # This will be the view to list the uploaded files (info)
                  new.html.erb     # This will be the form used to upload files
      models/
           upload.rb  # This is your model to represent an upload

Now that we have that in place, now the controller index method would automatically render the index.html.erb so there is no need for you to render the template directly. If you would like to see the list of uploaded document then may be you might need to the following your index controller method;

def index
  @uploads = Upload.all # this gets all upload information and makes it available for the view
end

and then you render then in your view, i.e. the index.html.erb file do the following to list them all

<ul>
  <%= @uploads.each do |upload| %>
    <li><%= upload.title %></li> <!-- I'm just using title here but you should change it to something that you have in your model -->
  <% end %>
</ul>

So gives you list of upload info on the index.html.erb page. Now the next thing would be to actually create upload info; that where the new action (a controller method), and new.html.erb come in. A word of caution here; I have never done a form that uploads files so I can not give you much detail here, but there is a good gem called paperclip so you might want to check that out for details but here is what I would given the information you provided above.

In your controller you need to add the following action:


def new
  @upload = Upload.new # This is a new upload model that you can use to create your upload form
end


then in you new.html.erb you add the following:

<% form_for(@upload) do |f| %>
  <p><label for="upload_file">Select File</label> :
  <%= file_field 'upload', 'datafile' %></p>
  <%= submit_tag "Upload" %>
<% end %>

I kinda feel this is a bit incomplete but I'm sure someone can build on this or just find another way to help you with your problem. I hope this helped in making things clearer. If you are in need of a good place to start with RoR go here, railstutorial.org. I can say it is the best and most complete book I've ever read on programming. It's that good!

Regards,
Tsega






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

No comments:

Post a Comment