Rails:为什么这个表单会触发错误的操作?
我的应用程序中有一个表单触发了错误的控制器操作。这是渲染的形式:
<form id="edit_profile_1" class="simple_form profile windowed" method="post" enctype="multipart/form-data" action="/profiles/1" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline">
<input type="hidden" value="✓" name="utf8">
<input type="hidden" value="put" name="_method">
<input type="hidden" value="..." name="authenticity_token">
</div>
....
</form>
所以,几乎是一个正常的形式。在我的本地环境中,这工作正常,它会触发 profiles#update
操作。但是,当由于某种原因部署在 Heroku 上时,这会触发 profiles#show
操作,因此不起作用。
什么给?有人以前遇到过这个错误吗?你知道如何修复它吗?
-编辑- @Laas:这是生产日志:
2011-05-20T21:41:38+00:00 app[web.1]: Started GET "/account" for 98.201.59.6 at 2011-05-20 14:41:38 -0700
2011-05-20T21:41:40+00:00 heroku[router]: GET www.fourthenvironment.org/account dyno=web.1 queue=0 wait=0ms service=2212ms bytes=8672
2011-05-20T21:41:40+00:00 app[web.1]: Connected to NewRelic Service at collector-6.newrelic.com:80
2011-05-20T21:41:40+00:00 heroku[router]: GET www.fourthenvironment.org/javascripts/rails.js dyno=web.1 queue=0 wait=0ms service=2ms bytes=5176
2011-05-20T21:41:41+00:00 heroku[router]: GET www.fourthenvironment.org/javascripts/jquery.144.min.js dyno=web.1 queue=0 wait=0ms service=3ms bytes=78865
2011-05-20T21:41:42+00:00 heroku[router]: GET www.fourthenvironment.org/stylesheets/style.css dyno=web.1 queue=0 wait=0ms service=3ms bytes=63444
2011-05-20T21:41:47+00:00 heroku[router]: GET www.fourthenvironment.org/favicon.ico dyno=web.1 queue=0 wait=0ms service=4ms bytes=1672
2011-05-20T21:41:50+00:00 app[web.1]:
2011-05-20T21:41:50+00:00 app[web.1]:
2011-05-20T21:41:50+00:00 app[web.1]: Started POST "/profiles/1" for 98.201.59.6 at 2011-05-20 14:41:50 -0700
2011-05-20T21:41:50+00:00 heroku[router]: POST www.fourthenvironment.org/profiles/1 dyno=web.1 queue=0 wait=0ms service=102ms bytes=420
2011-05-20T21:41:50+00:00 app[web.1]: THIS SHOULD NOT BE TRIGGERED
2011-05-20T21:41:50+00:00 heroku[router]: GET www.fourthenvironment.org/profiles/1 dyno=web.1 queue=0 wait=0ms service=30ms bytes=414
2011-05-20T21:41:50+00:00 app[web.1]:
2011-05-20T21:41:50+00:00 app[web.1]:
2011-05-20T21:41:50+00:00 app[web.1]: Started GET "/account" for 98.201.59.6 at 2011-05-20 14:41:50 -0700
注意“这不应该被触发”。这是控制器:
class ProfilesController < ApplicationController
before_filter :authenticate_user!
def show
puts "THIS SHOULD NOT BE TRIGGERED"
redirect_to account_path
end
def edit
@profile = Profile.find(params[:id])
end
def update
puts "profiles#update"
@profile = Profile.find(params[:id])
if @profile.update_attributes(params[:profile])
redirect_to account_path, :notice => t('user.notice.updated')
else
render :action => 'edit'
end
end
end
I have a form in my app which is triggering the wrong controller action. Here's the rendered form:
<form id="edit_profile_1" class="simple_form profile windowed" method="post" enctype="multipart/form-data" action="/profiles/1" accept-charset="UTF-8">
<div style="margin:0;padding:0;display:inline">
<input type="hidden" value="✓" name="utf8">
<input type="hidden" value="put" name="_method">
<input type="hidden" value="..." name="authenticity_token">
</div>
....
</form>
So, pretty much a normal form. On my local environment this works fine, it triggers the profiles#update
action. However, when deployed on Heroku for some reason this is triggering the profiles#show
action, and therefore is not working.
What gives? Has anyone encountered this error before, and do you know how to fix it?
-EDIT- @Laas: Here's the production log:
2011-05-20T21:41:38+00:00 app[web.1]: Started GET "/account" for 98.201.59.6 at 2011-05-20 14:41:38 -0700
2011-05-20T21:41:40+00:00 heroku[router]: GET www.fourthenvironment.org/account dyno=web.1 queue=0 wait=0ms service=2212ms bytes=8672
2011-05-20T21:41:40+00:00 app[web.1]: Connected to NewRelic Service at collector-6.newrelic.com:80
2011-05-20T21:41:40+00:00 heroku[router]: GET www.fourthenvironment.org/javascripts/rails.js dyno=web.1 queue=0 wait=0ms service=2ms bytes=5176
2011-05-20T21:41:41+00:00 heroku[router]: GET www.fourthenvironment.org/javascripts/jquery.144.min.js dyno=web.1 queue=0 wait=0ms service=3ms bytes=78865
2011-05-20T21:41:42+00:00 heroku[router]: GET www.fourthenvironment.org/stylesheets/style.css dyno=web.1 queue=0 wait=0ms service=3ms bytes=63444
2011-05-20T21:41:47+00:00 heroku[router]: GET www.fourthenvironment.org/favicon.ico dyno=web.1 queue=0 wait=0ms service=4ms bytes=1672
2011-05-20T21:41:50+00:00 app[web.1]:
2011-05-20T21:41:50+00:00 app[web.1]:
2011-05-20T21:41:50+00:00 app[web.1]: Started POST "/profiles/1" for 98.201.59.6 at 2011-05-20 14:41:50 -0700
2011-05-20T21:41:50+00:00 heroku[router]: POST www.fourthenvironment.org/profiles/1 dyno=web.1 queue=0 wait=0ms service=102ms bytes=420
2011-05-20T21:41:50+00:00 app[web.1]: THIS SHOULD NOT BE TRIGGERED
2011-05-20T21:41:50+00:00 heroku[router]: GET www.fourthenvironment.org/profiles/1 dyno=web.1 queue=0 wait=0ms service=30ms bytes=414
2011-05-20T21:41:50+00:00 app[web.1]:
2011-05-20T21:41:50+00:00 app[web.1]:
2011-05-20T21:41:50+00:00 app[web.1]: Started GET "/account" for 98.201.59.6 at 2011-05-20 14:41:50 -0700
Note the "THIS SHOULD NOT BE TRIGGERED". Here's the controller:
class ProfilesController < ApplicationController
before_filter :authenticate_user!
def show
puts "THIS SHOULD NOT BE TRIGGERED"
redirect_to account_path
end
def edit
@profile = Profile.find(params[:id])
end
def update
puts "profiles#update"
@profile = Profile.find(params[:id])
if @profile.update_attributes(params[:profile])
redirect_to account_path, :notice => t('user.notice.updated')
else
render :action => 'edit'
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实证明,该问题与 SSL-Requirement Gem 的使用有关。由于某种原因,如果您在使用 SSL 要求时将请求从 SSL 页面发送到非 SSL 页面,则会被重定向。因此,要修复此问题,请在请求的两端启用 SSL 要求,这样就可以正常工作。看起来从 SSL 到非 SSL,gem 只允许获取——因此出现了错误。从 Rails 3.1 开始,这看起来无关紧要,因为将内置新的 SSL 要求功能,并且不再需要 gem。
非常感谢 Heroku 工作人员帮助隔离此问题。
It turns out the issue was related to the use of the SSL-Requirement Gem. For some reason if you send a request from an SSL page to a non-SSL page while using SSL-requirement it gets redirected. So, to fix, enable SSL-requirement on both ends of the request, and it will work. It looks like going from SSL to non SSL the gem only allows gets -- hence the bug. This looks to be irrelevant as of Rails 3.1 since a new SSL requirement functionality will be baked-in and the gem will not be required.
Many thanks to the Heroku staff for helping isolate this.
似乎与此问题相同 routing-issues-with-multi-part -forms-on-heroku。
由于它才发布几天,也许这不是巧合,而是 Heroku 出了问题?
Seems to be identical to this problem routing-issues-with-multi-part-forms-on-heroku.
As it is only few days old, maybe it is not a coincidence and there's something awry with Heroku?
为什么表单标签是硬编码的?使用 form_tag 帮助器
http://api.rubyonrails.org/classes/ActionView/Helpers /FormTagHelper.html
Why is the form tag hard coded? Use the form_tag helper
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html