谢谢你花时间停下来。我有一个问题,我很困惑,但希望得到更好的理解。在我过去的项目中,我能做到为什么使用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)运行时,我将错误包含在顶部。看起来我收到了一个回滚事务。 –