Rails - Paperclip不保存用户头像

时间:2016-03-17 17:08:55

标签: ruby-on-rails paperclip

我安装了paperclip gem来管理app中的用户头像,但是当我尝试更改用户默认头像时,它不会改变。我没有收到任何错误,表格保存正确。似乎照片没有上传。

我做错了什么?我是新手,我无法弄明白。

型号:

getJSON('http://collidr-vidya.herokuapp.com/api/v1/scores').then(function (data) {
debugger;
alert('Your Json result is:  ' + data.scores); //you can comment this, i used it to debug

result.innerText = data.scores; //display the result in an HTML element
}, function (status) { //error detection....
alert('Something went wrong.');
});

控制器:

class User < ActiveRecord::Base
  has_many :posts
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/avatar.gif"
         validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end

修改视图:

class UsersController < ApplicationController
def create
  @user = User.create( user_params )
end
def update
    @user = User.find(params[:id])
    @user.update_attribute(:avatar, params[:user][:avatar])
end
private

# Use strong_parameters for attribute whitelisting
# Be sure to update your create() and update() controller methods.

def user_params
  params.require(:user).permit(:avatar)
end
end

我的路线:

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>
  <form>
    <div class="form-group">
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, class: "form-control" %>
  </div>

  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>

  <div class="field">
    <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
    <%= f.password_field :password, autocomplete: "off", class: "form-control" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
  </div>

  <div class="field">
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
  </div>
  <%= image_tag @user.avatar.url(:medium) %>
  <%= form_for @user, url: users_path, html: { multipart: true } do |form| %>
  <%= form.file_field :avatar %>
<% end %>
<br>
  <div class="actions">
    <%= f.submit "Update", class: "btn btn-success" %>
  </div>
<% end %>
</div></form>
<p><%= button_to "Delete my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete, class: "btn btn-danger" %></p>

来自DB的记录:

new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
              categories GET    /categories(.:format)          categories#index
                  topics GET    /topics(.:format)              topics#index
                         POST   /topics(.:format)              topics#create
               new_topic GET    /topics/new(.:format)          topics#new
              edit_topic GET    /topics/:id/edit(.:format)     topics#edit
                   topic GET    /topics/:id(.:format)          topics#show
                         PATCH  /topics/:id(.:format)          topics#update
                         PUT    /topics/:id(.:format)          topics#update
                         DELETE /topics/:id(.:format)          topics#destroy
                   posts GET    /posts(.:format)               posts#index
                         POST   /posts(.:format)               posts#create
                new_post GET    /posts/new(.:format)           posts#new
               edit_post GET    /posts/:id/edit(.:format)      posts#edit
                    post GET    /posts/:id(.:format)           posts#show
                         PATCH  /posts/:id(.:format)           posts#update
                         PUT    /posts/:id(.:format)           posts#update
                         DELETE /posts/:id(.:format)           posts#destroy
                   users GET    /users(.:format)               users#index
                         POST   /users(.:format)               users#create
                new_user GET    /users/new(.:format)           users#new
               edit_user GET    /users/:id/edit(.:format)      users#edit
                    user GET    /users/:id(.:format)           users#show
                         PATCH  /users/:id(.:format)           users#update
                         PUT    /users/:id(.:format)           users#update
                         DELETE /users/:id(.:format)           users#destroy
                    root GET    /                              categories#index

2 个答案:

答案 0 :(得分:1)

要为Devise注册添加新属性,您可以覆盖部分内容,而不是将婴儿扔出洗澡水:

# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
  before_filter :configure_permitted_parameters

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, [:avatar])
    devise_parameter_sanitizer.permit(:account_update, [:avatar])
  end
end

# config/routes.rb
devise_for :users, controllers: { registrations: "user/registrations" }

确保您的观看次数为app/views/users/registrations/new.html.erbapp/views/users/registrations/edit.html.erb

添加更多路线是一种天真的解决方案 - 你严重低估了Devise控制器实际完成的工作量。例如,您的控制器不会签署用户或甚至检查授权。

答案 1 :(得分:0)

感谢您对@max和@Kuba的建议。现在一切正常。我刚刚将以下代码添加到application_controller.rb

class ApplicationController < ActionController::Base
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception
    before_filter :configure_permitted_parameters, if: :devise_controller?

    protected

    def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_up) { |u| u.permit( :email, :password) }
        devise_parameter_sanitizer.for(:account_update) { |u| u.permit( :email, :password, :current_password, :avatar) }
    end
end