带设计的回形针

Paperclip with Devise

我在 AngularJS 中有一个带有前端应用程序的应用程序,向我的 Rails 后端应用程序发送 JSON 请求。我正在使用 Paperclip 和 Devise,User 模型的规范已更改以允许上传图像。

因为我已经有一个 image 属性,为了存储 Omniauth 登录返回的 url,我为我的 User 模型创建了一个新属性,使用命令:

rails g paperclip user stored_image

和运行随之而来的迁移。

我的user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, #:confirmable,
     :recoverable, :rememberable, :trackable, :validatable,
     :omniauthable, :omniauth_providers => [:facebook]

  has_attached_file :stored_image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/noimg.png"
  validates_attachment_content_type :stored_image, content_type: /\Aimage\/.*\Z/
end

我用来克服某些 Devise 限制的注册控制器如下

class RegistrationsController < Devise::RegistrationsController
  protected

  def update_resource(resource, params)
    # debugger
    resource.update_without_password(params)
  end
end

我的application_controller.rb

class ApplicationController < ActionController::Base
  respond_to :html, :json
  before_filter :configure_permitted_parameters, if: :devise_controller?
  after_filter :set_csrf_cookie_for_ng
  protect_from_forgery  #with: :null_session
  before_action :set_locale

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  def handle_unverified_request
    forgery_protection_strategy.new(self).handle_unverified_request
  end

  def set_csrf_cookie_for_ng
    cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery?
  end

  protected

  def verified_request?
    super || valid_authenticity_token?(session, request.headers['X-XSRF-TOKEN'])
  end

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:current_password ,:first_name, :last_name, :email, :password, :password_confirmation, :sex, :phone_number, :location, :birthday, :stored_image)}
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :sex, :phone_number, :location, :birthday, :stored_image ) }
    end
  end

需要澄清的是,用户仅在更新操作中上传他的图像​​。因此,当我在 update_resource 操作中调试并检查 params 时,我发现 stored_image 不存在,因此无法存储图像。我的 application_controller.rb 是否能让设备接受图片上传?

查看有关如何向 Devise 添加自定义字段的 THIS 文章。

This is because the way Rails 4 deals with "mass assignment." Due to security reasons we have to explicitly permit parameters inside each controller.

在您的 app/controllers/application_controller.rb 中明确允许您的参数 - 在您的情况下 stored_image:

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(:name, :email, :password) }
            devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password, :stored_image) }
        end
end

我的问题与强参数无关。出于某种原因,我必须首先将请求解析为 JSON,然后更新属性,在本例中,stored_image 属性