Rails/回形针新手 - 回形针无法保存

发布于 2024-10-11 11:49:59 字数 2154 浏览 2 评论 0原文

我对编程非常陌生,我正在尝试使用回形针将用户照片添加到我的用户记录中。可以从添加新记录表单创建记录,无需 <%= 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

控制器:

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

非常感谢任何帮助!

开发日志中显示的内容:

处理UsersController#create(用于 127.0.0.1 at 2011-01-12 22:05:56) [POST] 参数: {“用户”=>{“照片”=>#, “用户名”=>“nghjhg”, "密码"=>"ghjghj"}, "提交"=>"保存 变化”, “authenticity_token”=>“IlacpnqsC/iJ+41bx8tN4obOWPgirMx810l/WvohN68=”} [回形针] 识别 - 格式 %wx%h “C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png[0]” 2>NUL [回形针] 错误是 处理时收到:

C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png 未被“识别”识别 命令。> [回形针] 识别格式 %wx%h “C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png[0]” 2>NUL [回形针] 错误是 处理时收到:

C:/Users/Home/AppData/Local/Temp/stream110112-5292-2yorcw-0.png 未被“识别”识别 命令。>渲染模板内 布局/应用程序渲染 users/create 在 157 毫秒内完成(查看: 4、数据库:0)| 200 好 [http://localhost/users]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2024-10-18 11:49:59

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

编辑

我刚刚注意到你的控制器的这一部分:

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

这没有意义。如果第一次保存失败(返回 false),您只是再次尝试而不更改任何内容?我怀疑该行应该是 render :action => :新。

编辑 2

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文