简体   繁体   中英

Paperclip is not saving the avatar attached

I've done the migration I have

has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/missing/:style.png"

in my model, and im using

<%= form_for @user, :as => :user, :url => user_path, :html => { :multipart => true } do |form| %>
                <%= form.file_field :avatar %>
                <div class="actions">
                <%= form.submit %>
                </div>

To save the avatar uploaded and

<%= image_tag @user.avatar.url %>
<%= image_tag @user.avatar.url(:medium) %>
<%= image_tag @user.avatar.url(:thumb) %>

To display them all, but when I submit the new avatar it is not getting saved, I'm using

@user = User.create(params[:user])

in the create so it should be saving, what could be the problem?

<%= form_for @user, :validate => true, :html => { :multipart => true } do |f|%>
  <div class="field">
    <%= f.label :username %><br />
    <%= f.text_field :username, :disabled => 'disabled' %><br />

    <%= f.label :full_name %><br />
    <%= f.text_field :full_name%><br />

    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>

  <%= f.file_field :avatar %>

  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

ruby-1.9.2-p290 :002 > user = User.first
ruby-1.9.2-p290 :004 > user.avatar
=> /images/missing/original.png 

Nothing is getting changed

avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>

您需要将:avatar添加到attr_accessible列表中。

I assume that everything else for user is being saved properly?

The only thing that comes to my mind is, maybe you're using attr_accessible to protect from mass assignment and you forgot to add : avatar_file_name, :avatar_content_type, :avatar_file_size ... to the whitelist?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM