Rails 4 Paperclip默认网址无法正常工作

时间:2014-07-30 07:22:36

标签: ruby-on-rails-4 devise paperclip

我使用Rails 4,设计gem和paperclip gem,我的应用程序不想使用默认URL,如何让我的应用程序将默认回形针url保存到我的数据库中?

我必须补充一点,我希望我的应用为:superstarbadge:avatar回形针保存默认值。我希望我的:superstarbadgef.hidden_field :superstarbadge一起保存,我希望默认情况下为每个用户保存,但是当我尝试这样做时,我遇到no handler found for "/images/superbadge.jpeg"错误。

所以我有两个问题,
1。如何使用f.file_field :avatar将默认回形针网址保存到数据库
2。如何使用f.hidden_field :superstarbadge

将默认回形针网址保存到数据库

这是我的控制人员:

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_action :configure_devise_permitted_parameters, if: :devise_controller?

  protected

    def configure_devise_permitted_parameters
        registration_params = [:name, :email, :password, :password_confirmation,:avatar,:superstarbadge]

        if params[:action] == "update"
          devise_parameter_sanitizer.for(:account_update) {
            |u| u.permit(registration_params << :current_password)
          }
        elsif params[:action] == "create"
          devise_parameter_sanitizer.for(:sign_up) {
            |u| u.permit(registration_params)
          }
        end 
    end
end

这是我的用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :omniauthable
  # :recoverable, :rememberable and :trackable
  devise :database_authenticatable, :registerable, :validatable
  has_attached_file :avatar, :styles => { :small => "100x100>" }, :default_url => "/images/one.jpg"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

  has_attached_file :superstarbadge, :styles => { :small => "100x100>" }, :default_url => "/images/superbadge.jpeg" 
  validates_attachment_content_type :superstarbadge, :content_type => /\Aimage\/.*\Z/

  has_many :questions
  has_many :answers

  def to_s
    email
  end
end

我的设计注册表:

h1 Sign up

= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => { :multipart => true }) do |f|
  = devise_error_messages!

  .field
    label= f.label :name
    = f.text_field :name, autofocus: true

  .field
    label= f.label :email
    = f.email_field :email, autofocus: true

  .field
    label= f.label :password
    = f.password_field :password, autocomplete: 'off'

  .field
    label= f.label :password_confirmation
    = f.password_field :password_confirmation, autocomplete: 'off'

  .field
    = f.label :avatar
    = f.file_field :avatar

  .field 
    = f.hidden_field :superstarbadge
  div
    = f.submit "Sign up"

1 个答案:

答案 0 :(得分:0)

  1. 您在哪里存储样本图片? (来自app root的完整路径)
    1. 如果每个用户在创建时都收到相同的图片,为什么要使用隐藏字段? Paperclip将使用默认网址填充...
  2. 关于no handler found for "/images/superbadge.jpeg"的奇怪之处可能会尝试将其更改为.jpg?