感谢您花时间停下来。我有一个问题,我很困惑,但希望得到更好的理解。在我过去的项目中,我能够做到
<%= 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 :(得分: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
答案 1 :(得分:0)
您尝试使用似乎缺少正确验证的参数创建新用户,因此正在回滚提交。
您的用户模型需要密码才能创建新用户:
from collections import OrderedDict
import pprint
class ItemKey(object):
def __init__(self, name, position):
self.name = name
self.position = position
def __cmp__(self, b):
assert isinstance(b, ItemKey)
return cmp(self.position, b.position)
def __repr__(self):
return repr(self.name)
OrderedDict.items = lambda self: [
(ItemKey(name, i), value)
for i, (name, value) in enumerate(self.iteritems())]
OrderedDict.__repr__ = dict.__repr__
a = OrderedDict()
a[4] = '4'
a[1] = '1'
a[2] = '2'
print pprint.pformat(a) # {4: '4', 1: '1', 2: '2'}
但是,您的失败请求(来自您发布的终端错误)没有传入密码:
validates :password, presence: true, on: :create
如果您在控制器中修改新操作:
def new @user = User.new(密码:&#34;一些密码&#34;) 端
然后将@user放入form_for调用中,它应该可以正常工作。
编辑:我假设您正在寻找创建动作,但是,我认为Hristo Georgiev提供了正确的解决方案。