簡體   English   中英

帶設計的回形針

[英]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是否正確,以使設計器可以接受圖像上傳?

看看對如何添加自定義字段設計文章。

這是因為Rails 4處理“質量分配”的方式。 由於安全原因,我們必須明確允許每個控制器內的參數。

explicitly permit your parameter - in you case stored_image : 在您的明確允許您的參數-在您的情況下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屬性)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM