简体   繁体   中英

New to Rails/paperclip - Paperclip wont save

I'm very new to programming and I am trying to use paperclip to add a user photo to my user records. Records can be created from the add new record form without the <%= f.file_field :photo %> line and redirects properly and saves the record to the database. However, when its included on save it wants to redirect to create.html.erb instead of the users path and doesn't save the new record. It also doesn't display any errors. I have updated the users table with the photo_file_name, photo_content_type and :photo_file_size fields. Also, I'm running windows if thats any help.

Model:

class User < ActiveRecord::Base
  has_many :venues
  has_many :reviews
  has_attached_file :photo,
    :styles => { 
      :medium => "300x300>", 
      :thumb => "100x100>" }
end

Controller:

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def new
    @user = User.new
  end

  def create
    @user = User.create(params[:user])
    if @user.save
      flash[:notice] = 'User added'
      redirect_to users_path
    else
      @user.save
    end
  end

  def show
    @user = User.find(params[:id])
  end
end

View:

<% form_for (@user, :html => { :multipart => true }) do |f| %>

  <p>username: <br>
  <%= f.text_field :username %></p>

  <p>password: <br>
  <%= f.text_field :password %></p>

  <p>photo: <br>
  <%= f.file_field :photo %></p>

  <%= submit_tag %>
<% end %>

Any help is much appreciated!

Whats shown in the development log:

Processing UsersController#create (for 127.0.0.1 at 2011-01-12 22:05:56) [POST] Parameters: {"user"=>{"photo"=>#, "username"=>"nghjhg", "password"=>"ghjghj"}, "commit"=>"Save changes", "authenticity_token"=>"IlacpnqsC/iJ+41bx8tN4obOWPgirMx810l/WvohN68="} [paperclip] identify -format %wx%h "C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png[0]" 2>NUL [paperclip] An error was received while processing:

C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png is not recognized by the 'identify' command.> [paperclip] identify -format %wx%h "C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png[0]" 2>NUL [paperclip] An error was received while processing:

C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png is not recognized by the 'identify' command.> Rendering template within layouts/application Rendering users/create Completed in 157ms (View: 4, DB: 0) | 200 OK [http://localhost/users]

The use of :avatar in the docs for paperclip is just an example. In your case it should be :photo . You'll need to change that in both the model and the view files.

EDIT

I've only just noticed this part of your controller:

if @user.save
  flash[:notice] = 'User added'
  redirect_to users_path
else
  @user.save # <<< here
end

That makes no sense. If the first save fails (returns false) you're just trying it again without changing anything? I suspect that line should be render :action => :new .

EDIT 2

Your logs show that your identify command can't recognise .png files. Either that or you don't have an identify command. Did you install ImageMagick? If so, how?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM