SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
-
Feb 8, 2006, 09:45 #1
- Join Date
- Feb 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Syntax Questions (relative newbie)
Hey, I am working on Rails App and I am a bit wobbly on my syntax. I would like to send one of two kinds of send_file statements to a user depending on which kind of downloadable they ordered. I have basically just expanded on the depot code in the Agile Dev book.
Here is some of the code as it now stands.
Code:def download begin @order = Order.find(params[:id], :conditions => "ordered_at >= DATE_SUB(NOW(), INTERVAL 1 DAY)") rescue redirect_to_index("Sorry, that's an invalid order.") end begin @file = @order.line_items.find(:first, :conditions => ["deliverable_id = ?", params[:file]]) rescue redirect_to_index("Sorry, you did not order that file.") return end send_file "#{PDF_ROOT}/#{@file.deliverable.product.title.delete " "}.pdf", :type => 'application/pdf' end
What I would like to change is that last send_file bit to accomodate a statement like the following:
Code:if deliverable.type == 1 send_file "#{PDF_ROOT}/#{@file.deliverable_id " "}.pdf", :type => 'application/pdf' if deliverable.type == 2 send_file "#{MP3_ROOT}/#{@file.deliverable_id " "}.mp3", :type => 'audio/mpeg'
Code:if item.deliverable.download == 0
Thanks a ton, in advance. Peace.
-
Feb 8, 2006, 10:20 #2
Not equal would be != like in most other programming languages
-
Feb 8, 2006, 10:23 #3
To answer your last question, you have two options:
Code:if item.deliverable.download != 0
Code:unless item.deliverable.download == 0
For your main question, I would extract most of the logic to the model and I wouldn't bother storing the uploaded files in separate folders according to type because it makes things simpler and it makes little difference - the file can be stored anywhere and named anything (no file extension needed either) as long as you have a record of the content type in your database - this can be saved when its originally uploaded.
In the following example, attachments are stored in /var/www/attachments and they are saved using the format attachment_ID
The model:
Code:class Deliverable < ActiveRecord::Base def self.save_path(path) @@save_path = path end def attachment_path "#{@@save_path}/attachment_#{self.id}" end end
Code:Deliverable.save_path = '/var/www/attachments'
Code:def download begin @order = Order.find(params[:id], :conditions => "ordered_at >= DATE_SUB(NOW(), INTERVAL 1 DAY)") rescue redirect_to_index("Sorry, that's an invalid order.") end begin @file = @order.line_items.find(:first, :conditions => ["deliverable_id = ?", params[:file]]) rescue redirect_to_index("Sorry, you did not order that file.") return end send_file @file.deliverable.attachment_path, :type => @file.deliverable.content_type end
-
Feb 8, 2006, 10:25 #4
- Join Date
- Feb 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks, House. Like I said, I am completely novice. I would really dig any thoughts you might have on the first part of my question. Thanks again.
-
Feb 8, 2006, 10:25 #5
- Join Date
- Feb 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks luke, I will give it a go!
-
Feb 8, 2006, 10:29 #6
Here's an example taken from an app I'm working on at the moment, a knowledgebase module for part of a larger app, where knowledgebase articles can have many attachments:
Code:class KnowledgebaseArticleAttachment < ActiveRecord::Base belongs_to :article, :foreign_key => 'knowledgebase_article_id', :class_name => 'KnowledgebaseArticle' validates_presence_of :article, :message => 'is required' validates_presence_of :description after_save :store_file after_destroy :delete_file def self.save_path=(path) @@save_path = path end def uploaded_file=(file) @tmp_file = file self.attachment_name = sanitize_filename(file.original_filename) self.attachment_type = file.content_type self.attachment_size = File.size(file) end def file File.open(self.file_path, 'r') end def file_path "#{@@save_path}/KB#{self.id}" end protected def validate_on_create errors.add_to_base("You must upload a file") unless @tmp_file end private def store_file File.open(self.file_path, "wb") do |f| f.write(@tmp_file.read) end end def delete_file File.delete(self.file_path) end def sanitize_filename(file_name) # get only the filename, not the whole path (from IE) just_filename = File.basename(file_name) # replace all non-alphanumeric, underscore or periods with underscores just_filename.gsub(/[^\w\.\-]/,'_') end end
-
Feb 8, 2006, 10:47 #7
- Join Date
- Feb 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Luke, thanks a lot for your help. I am running into problems with the code's structure. I am a novice and trying to get this up and running without too much restructuring of the code/database (since I am so shaky and don't want to dig a deeper hole for myself). That being said is there no way (ruthlessly efficient or not) of keeping the logic basically as is, but having those last two options there at the end of the same controller? You are right about the keeping the files in the same file paths, though, and I will definitely do it that way.
In any case, I really appreciate it. Thanks.
-
Feb 8, 2006, 10:55 #8
Well the absolutely simplest thing to do given the code you originally posted is:
Code:case @file.deliverable.type when 1 send_file "#{PDF_ROOT}/#{@file.deliverable_id " "}.pdf", :type => 'application/pdf' when 2 send_file "#{MP3_ROOT}/#{@file.deliverable_id " "}.mp3", :type => 'audio/mpeg' end
-
Feb 8, 2006, 11:02 #9
- Join Date
- Feb 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks, a billion, Luke. I will give this a shot. I do think, though, that I will restructure the app in the future, but in the meantime I want to put this out into the world in a functioning state while I learn up on my Ruby knowledge. Thanks.
-
Feb 8, 2006, 16:20 #10
- Join Date
- Feb 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It worked perfectly. Thanks, Luke.
Bookmarks