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]%>
<%if !@message.upload.blank?%>
Uploaded File : <%= @message.upload.public_filename %>
<%[email protected]%>
<%end%>
…