gpt4 book ai didi

ruby-on-rails - Rails/paperclip 的新手 - Paperclip 不会保存

转载 作者:行者123 更新时间:2023-12-04 06:28:38 24 4
gpt4 key购买 nike

我是编程新手,我正在尝试使用回形针将用户照片添加到我的用户记录中。可以从没有 <%= f.file_field :photo %> 行的添加新记录表单创建记录,并正确重定向并将记录保存到数据库。但是,当它包含在保存时,它想重定向到 create.html.erb 而不是用户路径,并且不保存新记录。它也不显示任何错误。我已经用 photo_file_name、photo_content_type 和 :photo_file_size 字段更新了用户表。另外,如果有任何帮助,我正在运行 Windows。

型号:

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

查看:

<% 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 %>

非常感谢任何帮助!

开发日志中显示的内容:

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]

最佳答案

回形针文档中 :avatar 的使用只是一个例子。在您的情况下,它应该是 :photo。您需要在模型和 View 文件中更改它。

编辑

我刚刚注意到你 Controller 的这一部分:

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

这毫无意义。如果第一次保存失败(返回 false),你只是在不做任何更改的情况下再次尝试?我怀疑该行应该是 render :action => :new

编辑 2

您的日志显示您的 identify 命令无法识别 .png 文件。或者你没有 identify 命令。你安装了 ImageMagick 吗?如果是,怎么办?

关于ruby-on-rails - Rails/paperclip 的新手 - Paperclip 不会保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4653248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com