Paperclip无法保存到S3和数据库

时间:2017-08-13 05:21:50

标签: ruby-on-rails ruby heroku amazon-s3

宝石是 ·宝石'设计' ·宝石'回形针' ·gem' aws-sdk','〜> 2.3' rails版本是rails5 元数据不会保存在数据库中,也不会反映在S3中。 我尝试过各种研究并尝试过,但没有一项研究顺利。

数据库用户表

parseInt

模型/用户

 create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "provider"
    t.string "uid"
    t.string "name"
    t.string "phone_number"
    t.string "description"
    t.string "image"
    t.string "avatar_file_name"
    t.string "avatar_content_type"
    t.integer "avatar_file_size"
    t.datetime "avatar_updated_at"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

users_controller

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

   validates :name, presence: true,uniqueness: true, length: { maximum: 50 }
   validates :email, {presence: true, uniqueness: true}

   has_attached_file :avatar,
                     :styles => { :medium => "400x400", :thumb => "100x100>" },
                     :default_url => "avatar-default.png",
                     storage: :s3,
                     s3_credentials: {
                       bucket: ENV.fetch('S3_BUCKET_NAME'),
                       access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
                       secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
                       s3_region: ENV.fetch('AWS_REGION'),
                     }
   validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

 def self.from_omniauth(auth)
   where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
     user.email = auth.info.email
     user.password = Devise.friendly_token[0,20]
     user.name = auth.info.name   # assuming the user model has a name
     user.image = "http://graph.facebook.com/#{auth.uid}/picture?type=large" # assuming the user model has an image
     # If you are using confirmable and the provider(s) you use validate emails,
     # uncomment the line below to skip the confirmation emails.
     # user.skip_confirmation!
   end
 end
end

edit.html.erb

class UsersController < ApplicationController
 before_action :authenticate_user

  def edit
    @user = User.find_by(id: params[:id])
  end

  def update
    @user = User.find(params[:id])

    if @user.update(user_params)
      flash[:notice] = "can edit"
      redirect_to("/users/#{@user.id}")
    else
      flash[:notice] = "Can't edit"
      redirect_to("/users/#{@user.id}/edit")
    end
  end

 private

 def user_params
   params.require(:user).permit(:avatar, :name, :email, :phone_number, :description, :college_name)
 end

end

用于配置/环境开发和生产

<%= form_tag("/users/#{@user.id}/update", html:{multipart: true}) do %>

            <div class="form-group">
              <p>*name</p>
              <input name="user[name]" value="<%= @user.name %>" class="form-control">
            </div>

            <div class="form-group">
              <p>*email</p>
              <input name=user[email] value="<%= @user.email %>" class="form-control">
            </div>

            <div class="form-group">
              <p>avatar</p>
              <%= file_field_tag :avatar, class: "form-control" %>
            </div>

            <div class="form-group">
              <p>phone number</p>
              <input name=user[phone_number] value="<%= @user.phone_number %>" class="form-control">
            </div>

            <div class="form-group">
              <p>description</p>
              <textarea name=user[description] class="form-control" rows="8" cols="80"><%= @user.description %></textarea>
            </div>

            <div class="actions">
              <input type="submit" value="Edit" class="btn btn-primary">
            </div>

<% end %>

他的描述如下。 有关此内容的信息写在config / application.yml中。 我该如何编辑? 如果可能的话尽可能地告诉我,我将不胜感激。 谢谢。

0 个答案:

没有答案