简体   繁体   English

Ruby on Rails 4-回形针+ S3 +设计上传表单

[英]Ruby on Rails 4 - Paperclip + S3 + Devise upload form

I'm trying to set up avatar uploading to a Devise model, with Paperclip and S3 我正在尝试使用Paperclip和S3将头像上传到Devise模型

index.html.haml index.html.haml

= form_for(@user) do |f|
  - if @user.errors.any?
    #error_explanation
      %h2
        = pluralize(@user.errors.count, "error")
        prohibited this friend from being saved:
      %ul
        - @user.errors.full_messages.each do |msg|
          %li= msg
  .field
    = f.label :avatar
    %br
    = f.file_field :avatar
    %br
  .actions
    = f.submit 'Upload avatar'

routes.rb 的routes.rb

resources :users

paperclip.rb (the initializer, this is the whole content) paperclip.rb(初始化程序,这是全部内容)

Paperclip::Attachment.default_options[:storage] = :s3
Paperclip::Attachment.default_options[:s3_protocol] = 'http'
Paperclip::Attachment.default_options[:s3_credentials] =
    { :bucket => 'secret',
      :access_key_id => 'secret',
      :secret_access_key => 'secret' }
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'

index_controller.rb index_controller.rb

def index
  @user = User.new
end

production.rb & development.rb production.rb和development.rb

# Nothing to do with paperclip

So my problem is: When I'm uploading the image I get a Routing Error: uninitialized constant UsersController 所以我的问题是:当我上传图像时,我收到一个路由错误:未初始化的常量UsersController

I'm pretty new to Rails. 我对Rails很陌生。 What can I do? 我能做什么? Or does anyone have an example? 还是有人举一个例子?

You haven't created Users Controller and when you use form_for it use object's own routes. 您尚未创建用户控制器,而在使用form_for时,则使用对象自己的路由。

like in your case it becomes: 就像您的情况一样:

<form action="/users" method="POST">

In Routes it map on: 在“路线”中,它映射到:

users#create

You are checking it in IndexController that's wrong. 您正在IndexController中检查错误。 Your request send on UsersController and on create action. 您的请求在UsersController和create动作上发送。 You have to create users controller and create action in it. 您必须创建用户控制器并在其中创建操作。

users_controller.rb users_controller.rb

Like this: 像这样:

class UsersController < ApplicationController
def create
    @user = User.new
end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM