2011-01-11 72 views
3

我很新的編程,我想用回形針添加用戶照片到我的用戶記錄。可以通過添加新記錄表單創建記錄,而不需要<%= f.file_field:photo%>行並正確重定向並將記錄保存到數據庫。但是,當它包含在保存中時,它想要重定向到create.html.erb而不是用戶路徑,並且不保存新記錄。它也不顯示任何錯誤。我用photo_file_name,photo_content_type和:photo_file_size字段更新了用戶表。另外,如果有任何幫助,我正在運行窗口。新的導軌/回形針 - 回形針不會保存

型號:

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#創建(在22點05分56秒2011-01-12 127.0.0.1)[POST]參數: { 「用戶」= > {「photo」=>#, 「username」=>「nghjhg」, 「password」=>「ghjghj」},「commit」=>「保存 更改」, 「authenticity_token」=>「IlacpnqsC/iJ + 41bx8tN4obOWPgirMx810l/WvohN68 =「} [回形針] identify -format%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 不是由 '標識' 命令識別> [回形針]識別-format %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而處理所接收的不被''識別' 命令識別>在 佈局/應用程序中渲染模板渲染 用戶/創建在157ms內完成(查看: 4,DB:0)| 200 OK [HTTP://本地主機/用戶]

回答

3

在docs用於紙夾的使用:avatar僅僅是一個例子。你的情況應該是:photo。您需要在模型和視圖文件中更改它。

編輯

我剛剛注意到你的控制器的這部分:

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

這是沒有意義的。如果第一次保存失敗(返回false),您只需再次嘗試而不更改任何內容?我懷疑這條線應該是render :action => :new

EDIT 2

你的日誌顯示你identify命令無法識別.png文件。無論是或者您沒有identify命令。你安裝了ImageMagick嗎?如果是這樣,怎麼樣?

+0

感謝您的回答,只是注意到我在我的問題中犯了一個錯誤。我遷移用戶表與avater_file_name,avater_content_type和avatar_file_size字段不照片抱歉回合。現在它返回「缺少用戶模型attr_accessor for'photo_file_name'」。我已經用'照片'重新遷移,而且它的背面沒有保存。 – Dave 2011-01-12 20:54:40