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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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 <h2><%=@message.title%> </h2> <p><%=@message.summary%></p> <%if !@message.upload.blank?%> <p> Uploaded File : <%= @message.upload.public_filename %> </p> <p> </p> <p> <%=@message.body%> </p> <%end%> ......... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
showing the link is the problem? <%= link_to ''Download'', uploaded_file_path %> On Mar 24, 10:20 pm, Liam Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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 > > <h2><%...@message.title%> </h2> > <p><%...@message.summary%></p> > > <%if !...@message.upload.blank?%> > <p> > Uploaded File : <%= @message.upload.public_filename %> > </p> > <p> > > </p> > <p> > <%...@message.body%> > </p> > <%end%> > > ......... > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter wrote:> showing the link is the problem? > > <%= link_to ''Download'', uploaded_file_path %> > > > On Mar 24, 10:20 pm, Liam Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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...? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Peter wrote: > > showing the link is the problem? > > > <%= link_to ''Download'', uploaded_file_path %> > > > On Mar 24, 10:20 pm, Liam Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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...? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>FANTASTIC - THANK YOU for your help - very much appreciated! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---