謝謝你花時間停下來。我有一個問題,我很困惑,但希望得到更好的理解。在我過去的項目中,我能做到爲什麼使用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
你在使用設計寶石嗎?通過查看終端,看看通過表單發送了什麼樣的參數也是一件好事。由於某種原因,密碼參數可能會被過濾。 另一個有用的方法是隻放置用戶變量(<%= @user %>)並查看它包含的內容。 –
「@ user」被分配在哪裏?你可以發佈這個視圖的控制器代碼,以便我們可以更好地理解這個問題。它似乎可能是一個簡單的驗證問題,但我不確定爲什麼它會在current_user的實例中跳過它(也許張貼用戶模型的相關部分) – engineersmnky
@HristoGeorgiev我沒有使用設計寶石。把(<%= @ user %>),我得到<用戶:0x007fabffd58b28>和終端內沒有。所有文件運行沒有任何錯誤。使用(form_for @ user)運行時,我將錯誤包含在頂部。看起來我收到了一個回滾事務。 –