我想向用户添加头像我做了一些步骤,但是当我更新用户时,我选择了照片,但它没有更新我不知道问题是什么 每个其他属性都会成功更新。
edit.html.erb
<%= form_for(current_user,:html => { :multipart => true }) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %><br>
<%= f.text_field :username%>
</div>
<div class="field">
<%= f.label :Fullname %><br>
<%= f.text_field :Fullname%>
</div>
<div class="field">
<%= f.label :company %><br>
<%= f.text_field :company %>
</div>
<div class="field">
<%= f.label :age %><br>
<%= f.number_field :age %>
</div>
<div class="field">
<%= f.label :like_number %><br>
<%= f.number_field :like_number %>
</div>
<div class="field">
<%= f.label :dislike_number %><br>
<%= f.number_field :dislike_number %>
</div>
<div class="field">
<%= f.label :user_points %><br>
<%= f.number_field :user_points %>
</div>
<div class="field">
<%= f.label :avatar %><br>
<%= f.file_field :avatar %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
users_controller
def edit
end
def user_params
params.require(:user).permit(:avatar, :age,:company,:username, :Fullname, :email, :password, :gender, :like_number, :dislike_number, :user_points)
end
def update
respond_to do |format|
if current_user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
users model
#every user has an image (avatar)
attr_accessor :avatar_file_name
attr_accessor :avatar_content_type
attr_accessor :avatar_file_size
attr_accessor :avatar_updated_at
#has_attached_file :avatar,
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
migration
class Avatar < ActiveRecord::Migration
# save an image (avatar) of type attachment to user table in database
class AddAttachmentAvatarToProfiles < ActiveRecord::Migration
def change
add_attachment :users, :avatar
end
end
end
gemfile
gem 'xpath', '~> 2.0.0'
#to upload a photo use paperclip gem.
#to use this gem you should download ImageMagick.
#to download ImageMagick :
# sudo apt-get update
# sudo apt-get install imagemagick
gem 'paperclip', '~> 3.0'
controller
def show
end
show.html.erb
<!--check if the user has a photo or not -->
<!--if user hasn't a photo , display the defult photo -->
<!--if user has photo , display it -->
<%if current_user.avatar.blank? %>
<%if current_user.gender == 'Female'%>
<img class="language-flag" src="/assets/girl.jpg" titile="Change Photo">
<%else%>
<img class="language-flag" src="/assets/boy.jpg" titile="Change Photo">
<% end %>
<%else%>
<%= image_tag current_user.avatar.url(:large) %>
<%end%>
答案 0 :(得分:0)
你想让化身真正出现在哪里?我在user#show
猜测?如果是这样,你也可以发布该代码。我猜你正在保存/上传好的东西,但只是没有从数据库中抓取东西并实际将它们发送到视图。