Rails 3 & Paperclip issue
I am very new to rails and trying to get paperclip working for user profiles on my site. I setup the paperclip gem in my gemfile and bundle installed. I was having an issue using :has_attached_file so I then used 'rails plugin install github/path/to/paperclip.git' and restarted webrick. After that everything seemed to be working as far as rendering my new form and giving me the option to upload a file.
When I try and upload a photo to a user profile it just takes me back to the page for the user without telling me it was successfully updated and without showing my newly uploaded image. It does adjust the name or email address if you change those. It doesn't look like it actually puts the image into the path that is listed on the readme on Github for paperclip. Is here some config file i'm not seeing?
I ran 'rails generate paperclip user photo' and below is some of the pertinent code:
From models/user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email
email_regex = /\A[\开发者_运维问答w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
#code below tells rails that our users have photos
has_attached_file :photo
end
From views/users/show.html.erb:
<b>Username: </b><%= @user.name %>
<p><b>Email Address: </b><%= @user.email %></p>
<p><b>Avatar: </b><% if @user.photo? %><%= image_tag @user.photo.url %><% else %>No Photo Uploaded!<% end %></p>
From views/users/_form.html.erb
<% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |f| %>
<p>
<%= f.label :name.to_s << ': ' %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label 'Email Address:' %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :Avatar.to_s << ': ' %>
<%= f.file_field :photo %>
</p>
<p>
<%= f.submit 'Save' %>
</p>
<% end %>
Edit: adding controller code: From controllers/users_controller.rb:
class UsersController < ApplicationController
before_filter :authenticate, :except => [:index]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
**code removed for brevity**
end
end
end
Some questions: did you run rake db:migrate
after rails generate paperclip user photo
? In your controller do you actually have a show
method that loads the saved user and thus displays the photo?
Also to display the :notice
message you need to add it somewhere in your application template with <%= flash[:notice] =>
.
I've gotten this fixed. The issue seems to have been a problem with the _form partial I was using on the new user and edit user screen. I've left the bad template code in my original question and I'll post the fixed code here. The :user and :url => values were removed.
From /views/user/_form.html.erb
<% form_for @user, :html => { :multipart => true } do |f| %>
<p>
<%= f.label :name.to_s << ': ' %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label 'Email Address:' %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :Avatar.to_s << ': ' %>
<%= f.file_field :photo %>
</p>
<p>
<%= f.submit 'Save' %>
</p>
<% end %>
精彩评论