"how to" show link to uploaded pdf file - newbie

Working with “attachment_fu” and have successfully uploaded a PDF file
via a form.
(File is located in /public/uploads/ )

I am having trouble “showing” the link to the file in the show page -
could someone show me some example code for the controller, model,view
etc… ?
The goal is to have a link to a PDF file associated with a blog entry.
Many thanks

Is it already linked to a post in the models? (pdf_file
belongs_to :post, post :has_one pdf_file)

of so, you can write…

post.pdf_file.public_filename

…to get the path to the uploaded file

On Mar 24, 8:50 pm, Liam S. [email protected]

Peter wrote:

Is it already linked to a post in the models? (pdf_file
belongs_to :post, post :has_one pdf_file)

of so, you can write…

post.pdf_file.public_filename

…to get the path to the uploaded file

On Mar 24, 8:50 pm, Liam S. [email protected]

Here’s the result (show.rhtml) of the code that I am trying to use.
Unfortunately it just shows the text string of the path and not the link
to the actual file.


(localhost:3000/messages/12)

Title

Summary

Uploaded File : /uploads/0000/0010/test.pdf

Body text

And here is the actual code…

controller =

class MessagesController < ApplicationController

def index
@title = “Messages”
@messages = Messages.find(:all)

end

def create
#Create the message
@message = Message.new(params[:message])
respond_to do |format|
if @message.save

    if !params[:upload][:uploaded_data].blank?
      @message.upload = Upload.create(params[:upload])
    end
    flash[:notice] = 'File was successfully uploaded.'
    format.html { redirect_to :action=>'show', :id=>@message }
  else
    format.html { render :action => "new" }
  end
end

end

def show
@message = Message.find(params[:id])

respond_to do |format|
  format.html # show.rhtml
  format.xml  { render :xml => @message.to_xml }
end

end

def update
@message = Message.find(params[:id])
respond_to do |format|
if @message.update_attributes(params[:message])

      if !params[:upload][:uploaded_data].blank?
        #find current upload
        @upload = @message.upload ||= Upload.new
        @upload = @message.upload.build(params[:upload])
        @upload.save
      end
      format.html { redirect_to :action=>'show', :id=>@message }
    else
      format.html { render :action => "edit" }
    end
  end
end

end

message model =

class Message < ActiveRecord::Base
has_one :upload, :conditions => ‘parent_id is null’
has_one :all_uploads, :class_name => ‘Upload’ # all attachments
end


upload model =

class Upload < ActiveRecord::Base

has_attachment :storage => :file_system,
:max_size => 1.megabytes

validates_as_attachment

belongs_to :message
end


view show.rhtml =

<%[email protected]%>

<%[email protected]%>

<%if !@message.upload.blank?%>

Uploaded File : <%= @message.upload.public_filename %>

<%[email protected]%>

<%end%>

Peter wrote:

showing the link is the problem?

<%= link_to ‘Download’, uploaded_file_path %>

On Mar 24, 10:20 pm, Liam S. [email protected]

Yes, I am not understanding (sorry about this!) - how to code the
“uploaded_file_path” - how do I define this/where do I define this,
etc…?

showing the link is the problem?

<%= link_to ‘Download’, uploaded_file_path %>

On Mar 24, 10:20 pm, Liam S. [email protected]

Peter wrote:

that should be:

message.upload.public_filename

where message can be some message you find with Message.find
params[:id]

On Mar 25, 12:16 am, Liam S. [email protected]

FANTASTIC - THANK YOU for your help - very much appreciated!

that should be:

message.upload.public_filename

where message can be some message you find with Message.find
params[:id]

On Mar 25, 12:16 am, Liam S. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs