Ruby on rails Rails教程-添加图像上载验证时,开发中出现异常重新输入错误
更新 今天早上,我注意到heroku日志中略高一点的以下列表:Ruby on rails Rails教程-添加图像上载验证时,开发中出现异常重新输入错误,ruby-on-rails,ruby,amazon-web-services,heroku,development-environment,Ruby On Rails,Ruby,Amazon Web Services,Heroku,Development Environment,更新 今天早上,我注意到heroku日志中略高一点的以下列表: excon.error.response 2016-11-06T18:17:48.973789+00:00 app[web.1]: :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message&
excon.error.response
2016-11-06T18:17:48.973789+00:00 app[web.1]: :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-1'</Message><Region>us-west-1</Region><RequestId>...</RequestId><HostId>...</HostId></Error>"
excon.error.response
2016-11-06T18:17:48.973789+00:00 app[web.1]: :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-1'</Message><Region>us-west-1</Region><RequestId>...</RequestId><HostId>...</HostId></Error>"
将:region=>ENV['S3_region']
添加到我的fog凭据后,然后在命令行上运行heroku config:set S3_region=us-west-1
,图片上载正在生产中运行!因此,我的问题大约有一半得到了解决——我仍然不完全理解为什么create操作会在生产和开发中给我带来问题——特别是考虑到这两个环境的配置完全不同。我的怀疑是,在开发过程中,if@micropost.save
会造成问题,因为错误被删除了几层——在亚马逊的web服务中。对于这个问题,我没有一个明确的答案,但我要标记它已经解决了——实际上,它已经解决了
我即将结束Michael Hartl的Ruby on Rails教程,就在我即将实现图像上传到用户MicroPost的时候,我遇到了一个非常隐秘的错误:异常重新进入
似乎认为问题在于我在microposts控制器下的创建操作:
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
...
private
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || root_url
end
private
def micropost_params
params.require(:micropost).permit(:content, :picture)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
具体地说,它突出显示了if@micropost.save行
现在,在网上找不到什么帮助后,我使用了我最基本的粘贴Michael的“代码覆盖我的代码”技术,令人惊讶的是,我的错误消失了。我将其缩小到micropost模型,这会导致一个问题:
class Micropost < ApplicationRecord
belongs_to :user
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 140 }
validate :picture_size
private
# Validates the size of an uploaded picture.
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5MB")
end
end
end
所以我不能只是把手擦干净然后继续前进。这就是我的问题:
-广义地说,“异常重新输入”错误是什么?
-我的压痕理论可信吗?
-你知道我该怎么解决这个问题吗
非常感谢你,我是个新手,所以如果你需要更多信息,请告诉我(如果这是一个愚蠢的错误,请耐心等待)
作为参考,全micropost控制器:
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
...
private
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || root_url
end
private
def micropost_params
params.require(:micropost).permit(:content, :picture)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
class MicropostsController
更新我今天早上注意到,heroku日志中的以下列表略高一些:
excon.error.response
2016-11-06T18:17:48.973789+00:00 app[web.1]: :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-1'</Message><Region>us-west-1</Region><RequestId>...</RequestId><HostId>...</HostId></Error>"
excon.error.response
2016-11-06T18:17:48.973789+00:00 app[web.1]: :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-1'</Message><Region>us-west-1</Region><RequestId>...</RequestId><HostId>...</HostId></Error>"
将
:region=>ENV['S3_region']
添加到我的fog凭据后,然后在命令行上运行heroku config:setS3_region=us-west-1
,图片上载正在生产中运行!因此,我的问题大约有一半得到了解决——我仍然不完全理解为什么create操作会在生产和开发中给我带来问题——特别是考虑到这两个环境的配置完全不同。我的怀疑是,在开发过程中,if@micropost.save
会造成问题,因为错误被删除了几层——在亚马逊的web服务中。我对这个问题没有一个明确的答案,但我会将它标记为已解决-实际上,它是。更新我今天早上注意到heroku日志中更高一点的以下列表:
excon.error.response
2016-11-06T18:17:48.973789+00:00 app[web.1]: :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-1'</Message><Region>us-west-1</Region><RequestId>...</RequestId><HostId>...</HostId></Error>"
excon.error.response
2016-11-06T18:17:48.973789+00:00 app[web.1]: :body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-1'</Message><Region>us-west-1</Region><RequestId>...</RequestId><HostId>...</HostId></Error>"
将
:region=>ENV['S3_region']
添加到我的fog凭据后,然后在命令行上运行heroku config:setS3_region=us-west-1
,图片上载正在生产中运行!因此,我的问题大约有一半得到了解决——我仍然不完全理解为什么create操作会在生产和开发中给我带来问题——特别是考虑到这两个环境的配置完全不同。我的怀疑是,在开发过程中,if@micropost.save
会造成问题,因为错误被删除了几层——在亚马逊的web服务中。我对这个问题没有一个明确的答案,但我将标记它已解决-事实上,它已解决。请发布micropost controllercan您可以在本地运行并显示完整的堆栈跟踪吗?在生产环境中进行调试会更加困难boxes@RajarshiDas-我添加了控制器。@maxpleaner-不幸的是,我无法在我的开发服务器中恢复错误!“异常重新输入”错误在哪里?如果问题中没有显示,请发布micropost controllercan您可以在本地运行并显示完整的堆栈跟踪吗?在生产环境中进行调试会更加困难boxes@RajarshiDas-我添加了控制器。@maxpleaner-不幸的是,我无法在我的开发服务器中恢复错误!“异常重新输入”错误在哪里?你在问题中没有表现出来