【问题标题】:Rails 4 Paperclip default url not workingRails 4 Paperclip默认网址不起作用
【发布时间】:2014-07-30 07:22:35
【问题描述】:

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

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

所以我有两个问题,
1。如何将默认回形针 url 保存到数据库 WITH 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"

【问题讨论】:

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


    【解决方案1】:
    1. 您将示例图像存储在哪里? (来自应用根目录的完整路径)
      1. 如果每个用户在创建时都收到相同的图像,为什么还要使用隐藏字段? Paperclip 将使用默认 url 填充它...

    no handler found for "/images/superbadge.jpeg" 的奇怪之处也许可以尝试将其更改为 .jpg ?

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2013-12-23
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多