Michael Klein
2005-Apr-23 03:04 UTC
Uploading a File and an Easy "link_to" Question, Please Help
My problem comes with uploading a picture on my website. Right now I would like to have a form where a user can upload a picture and the server will move that picture to public/images. After the picture is moved I would like to store the path to this file in my MySQL server - so potentially I could just call something like: <img src="<%= @post.file %>" in one of my views. If possible, I would also like to validate that the uploaded file is an actual JPEG/JPG file under 5 megabytes. I have included any information that could potentially be helpful. Absolute path of public/images: /mnt/lshare/www/ruby/photoblog/app/public/images app/controllers/post_controller.rb: class PostController < ApplicationController before_filter :login_required, :except => [:index] def new @post = Post.new end def create @post = Post.new(@params["post"]) if @post.save redirect_to :action => "show", :id => @post.id.to_s else render_action ''new'' end end def show @post = Post.find(@params["id"]) end end views/post/new.rhtml: <form action="/post/create" method="post" enctype="multipart/form-data"> <p><label for="title">Title:</label><br /> <%= text_field "post", "title" %></p> <p><label for="description">Description:</label><br /> <%= text_field "post", "description" %></p> <p><label for="file">File:</label><br /> <% file_field "post", "file" %><input id="post_file" name="post[file]" type="file"></p> <p><input type="submit" value="Post It."></p> <%= end_form_tag %> photoblog.sql: CREATE TABLE posts ( id int(6) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, title text NOT NULL, description text, created_on timestamp NOT NULL, updated_on timestamp NOT NULL, file text NOT NULL ); CREATE TABLE users ( id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, login varchar(80) default NULL, password varchar(40) default NULL ); And just one more actually easy question! When I show the picture, I would like a link to the previous picture. I have tried to doing this by putting the id as (@params["id"] - 1) but that doesn''t work. It might be a syntax error or something. views/post/show.rhtml: <p>The title is: <%= @post.title %></p> <p>The description is: <%= @post.description %></p> <p>The post was made on: <%= @post.created_on.strftime "%e %B %Y | %I:%M %p" %></p> <p>The file is:<%= @post.file %> <br /> <br /> <p><%= link_to "Previous post.", :action => "show", id => (@params["id"] - 1) %> If you get sick of looking at all this e-mailed code the actual project can be found: http://swilly.tk/~klein/RoRphotoblog.zip (Does not include the database file.) A sincere thanks to everyone who can help! If you can only help with one of the two questions I''m still very thankful. --- [This E-mail scanned for viruses by Declude Virus]