我正在尝试使用Paperclip和S3将头像上传到Devise模型
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
resources :users
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
def index
@user = User.new
end
production.rb和development.rb
# Nothing to do with paperclip
所以我的问题是:当我上传图像时,我收到一个路由错误:未初始化的常量UsersController
我对Rails很陌生。 我能做什么? 还是有人举一个例子?
您尚未创建用户控制器,而在使用form_for时,则使用对象自己的路由。
就像您的情况一样:
<form action="/users" method="POST">
在“路线”中,它映射到:
users#create
您正在IndexController中检查错误。 您的请求在UsersController和create动作上发送。 您必须创建用户控制器并在其中创建操作。
users_controller.rb
像这样:
class UsersController < ApplicationController
def create
@user = User.new
end
end