2015-08-24 70 views
0

謝謝你花時間停下來。我有一個問題,我很困惑,但希望得到更好的理解。在我過去的項目中,我能做到爲什麼使用form_for時它必須是current_user而不是@user

<%= form_for @user, :html => { :multipart => true } do |f| %> 
<p class="editpage">Profile Picture: <%= f.file_field :avatar %></p> 
<%= f.submit %> 
<% end %> 

但在我目前的項目。當我使用@user時,當我嘗試上傳圖片時,它會一直說要求的密碼。我有我的用戶控制器的私人蔘數中有:密碼和:password_confirmation。目前,我想通了,上傳的圖片沒有問題的唯一方法是用

<%= form_for current_user, :html => { :multipart => true } do |f| %> 
    <p class="editpage">Profile Picture: <%= f.file_field :avatar %></p> 
    <%= f.submit "Upload" %> 
<% end %> 

有人能幫助解釋爲什麼這已發生。我很高興它以某種方式工作,但我想更好地瞭解爲什麼它需要成爲current_user而不是@user,因爲我試圖更新用戶信息。感謝您的幫助和解釋。

新增信息,我的ApplicationController內內終端

helper_method :current_user 


def current_user 
if session[:user_id] 
    @current_user = User.find(session[:user_id]) 
else 
    @current_user = nil 
end 
end 

錯誤

Started POST "/users" for 127.0.0.1 at 2015-08-25 02:04:55 +0900 
Processing by UsersController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"e3DrDAhJPV4vgma9tg7wk9g589ZRDNInynWlXDv7oJQ=", "user"=> {"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f8784a00ec0 @tempfile=# <Tempfile:/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/RackMultipart20150825- 4587-14jh732>, @original_filename="10920917_10152994488342090_418253413398867864_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"10920917_10152994488342090_418253413398867864_n.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Upload"} 
Command :: file -b --mime '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c7 20150825-4587-1ka91nc.jpg' 
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c7 20150825-4587-1p9nueh.jpg[0]' 2>/dev/null 
Command :: identify -format %m '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' 
Command :: convert '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' -auto-orient -resize "200x200>" '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh20150825-4587-1alffdu' 
Command :: file -b --mime '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh20150825-4587-1alffdu' 
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' 2>/dev/null 
Command :: identify -format %m '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' 
Command :: convert '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' -auto-orient -resize "120x" -crop "120x120+0+20" +repage '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh20150825-4587-1kd13uh' 
Command :: file -b --mime '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh20150825-4587-1kd13uh' 
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' 2>/dev/null 
Command :: identify -format %m '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' 
Command :: convert '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' -auto-orient -resize "75x" -crop "75x75+0+12" +repage '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh20150825-4587-1recdb2' 
Command :: file -b --mime '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh20150825-4587-1recdb2' 
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' 2>/dev/null 
Command :: identify -format %m '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' 
Command :: convert '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh.jpg[0]' -auto-orient '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh20150825-4587-1vsru58' 
Command :: file -b --mime '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1p9nueh20150825-4587-1vsru58' 
    (0.4ms) begin transaction 
    Command :: file -b --mime '/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/47a027085753557774697c8014ad69c720150825-4587-1ksrnek.jpg' 
    (0.1ms) rollback transaction 
Redirected to http://localhost:3000/ 
Completed 302 Found in 625ms (ActiveRecord: 0.5ms) 

** UserController的*

class UsersController < ApplicationController 
def index 
    @user = User.new 
    @users = User.all 
    # if current_user 
    # @leaders = @current_user.leaders 
    # end 
end 

def create 
    @user = User.new(user_params) 
    if @user.save 
    session[:user_id] = @user.id 
    cookies[:user_id] = @user.id 
    flash[:notice] = "Successfully Registerd" 
    redirect_to "/" 
    else 
    flash[:alert] = @user.errors.full_messages 
    redirect_to "/" 
    end 
end 

def new 
    @user = User.new 
end 

def edit 
    @user = User.friendly.find(params[:id]) 
    current_user 
end 

def show 
    @user = User.friendly.find(params[:id]) 
    current_user 
    # if @current_user 
    # @followerlink = Follow.where(leader_id: @user.id, 
    #        follower_id: @current_user.id).first 
    # end 
end 

def update 
    @user = User.friendly.find(params[:id]) 
    if @user.update(user_params) 
    flash[:notice] = "You have successfully update your information" 
    redirect_to "/" 
    end 
end 

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


def user_params 
    params.require(:user).permit(:background, :username_or_email, :first_name, :last_name, :email, :password, :password_confirmation, :user_name, :avatar) 
end 
end 

UserModel

class User < ActiveRecord::Base 

    extend FriendlyId 
    friendly_id :user_name, use: :slugged 

    has_many :conversations, :foreign_key => :sender_id 
    after_create :create_default_conversation 

    has_attached_file :avatar, :styles => { 
    :medium => "200x200>", 
    :small => "120x120#", 
    :thumb => "75x75#", 
    :default_url => "http://www.adtechnology.co.uk/images/UGM-default-user.png" 
    } 

    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 

    has_attached_file :background, :styles => { 
    :medium => "200x200>", 
    :small => "120x120#", 
    :thumb => "75x75#", 
    :default_url => "http://www.adtechnology.co.uk/images/UGM-default-user.png" 
    } 

    validates_attachment_content_type :background, :content_type => /\Aimage\/.*\Z/ 

    validates :password, presence: true, on: :create 

end 
+0

你在使用設計寶石嗎?通過查看終端,看看通過表單發送了什麼樣的參數也是一件好事。由於某種原因,密碼參數可能會被過濾。 另一個有用的方法是隻放置用戶變量(<%= @user %>)並查看它包含的內容。 –

+0

「@ user」被分配在哪裏?你可以發佈這個視圖的控制器代碼,以便我們可以更好地理解這個問題。它似乎可能是一個簡單的驗證問題,但我不確定爲什麼它會在current_user的實例中跳過它(也許張貼用戶模型的相關部分) – engineersmnky

+0

@HristoGeorgiev我沒有使用設計寶石。把(<%= @ user %>),我得到<用戶:0x007fabffd58b28>和終端內沒有。所有文件運行沒有任何錯誤。使用(form_for @ user)運行時,我將錯誤包含在頂部。看起來我收到了一個回滾事務。 –

回答

2

您正在使用此表單創建用戶。

Processing by UsersController#create as HTML 

而既然你只是想添加一個化身,那麼你就是想要做一個更新。您需要指定要向服務器發出的請求類型,因此請將:method => :PUT添加到您的表單中。

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

此外,你需要有任何有密碼的has_secure_password方法。 Read about it here。很高興知道它是什麼以及它做了什麼。

has_secure_password 
validates :password, presence: true, allow_nil: true 

我增加了一個allow_nil: true,以便您可以更新而無需指定密碼。你可以調試發生了什麼你的變量

一種方法是使用inspect

例如:

@user.inspect 
    current_user.inspect 

如果你希望看到什麼樣的屬性,你的模型,你可以使用它作爲以及:

User.inspect 
0

您試圖創建一個新用戶,其參數看起來缺少適當的驗證,所以提交正在回滾。

您的用戶模式需要密碼是存在於創建新用戶:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"e3DrDAhJPV4vgma9tg7wk9g589ZRDNInynWlXDv7oJQ=", "user"=> {"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f8784a00ec0 @tempfile=# <Tempfile:/var/folders/83/2r02tq2d3934d30syfmpz9yw0000gn/T/RackMultipart20150825- 4587-14jh732>, @original_filename="10920917_10152994488342090_418253413398867864_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"10920917_10152994488342090_418253413398867864_n.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Upload"} 

validates :password, presence: true, on: :create 

然而,發生故障的請求(從您發佈的終端錯誤)沒有密碼傳遞

如果您修改控制器的新動作:

高清新 @user = User.new(密碼: 「一些密碼」) 結束

,然後把@user在你的form_for調用,它應該工作。

編輯:我假設您正在尋找創建操作,但是,我認爲Hristo Georgiev提供了正確的解決方案。

+0

目前的問題是當我試圖更新用戶圖片時,如果我使用form_for @ user,我得到的密碼不能爲空。 Form_for current_user在上傳圖片時沒有問題。我也認爲Hristo有正確的想法,但我仍然得到一個有關密碼空白的驗證問題。 –

相關問題