简体   繁体   English

Rails / paperclip新手 - Paperclip不会保存

[英]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. 我是编程的新手,我正在尝试使用paperclip将用户照片添加到我的用户记录中。 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. 可以从添加新记录表单创建记录,而不使用<%= f.file_field:photo%>行并正确重定向并将记录保存到数据库。 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. 但是,当它包含在save中时,它想要重定向到create.html.erb而不是用户路径,并且不保存新记录。 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. 我已使用photo_file_name,photo_content_type和:photo_file_size字段更新了users表。 Also, I'm running windows if thats any help. 此外,如果有任何帮助,我正在运行Windows。

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: 处理UsersController #create(对于127.0.0.1于2011-01-12 22:05:56)[POST]参数:{“user”=> {“photo”=>#,“username”=>“nghjhg”,“密码“=>”ghjghj“},”commit“=>”保存更改“,”authenticity_token“=>”IlacpnqsC / iJ + 41bx8tN4obOWPgirMx810l / WvohN68 =“} [paperclip]识别-format%wx%h”C:/ Users /Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png[0]“2> NUL [paperclip]处理时收到错误:

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无法通过'identify'命令识别。> [paperclip] identify -format%wx%h“C:/ Users / Home /AppData/Local/Temp/stream110112-5292-2yorcw-0.png[0]“2> NUL [paperclip]处理时收到错误:

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) | C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png无法通过'identify'命令识别。>在布局/应用程序中渲染模板渲染用户/创建在157ms内完成(查看:4 ,DB:0)| 200 OK [http://localhost/users] 200 OK [http:// localhost / users]

The use of :avatar in the docs for paperclip is just an example. 在回形针的文档中使用:avatar只是一个例子。 In your case it should be :photo . 在你的情况下,它应该是: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? 如果第一次保存失败(返回false),你只是再次尝试而不改变任何东西? I suspect that line should be render :action => :new . 我怀疑该行应该是render :action => :new

EDIT 2 编辑2

Your logs show that your identify command can't recognise .png files. 您的日志显示您的identify命令无法识别.png文件。 Either that or you don't have an identify command. 要么,要么你没有identify命令。 Did you install ImageMagick? 你安装了ImageMagick吗? If so, how? 如果是这样,怎么样?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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