Rails form_for 在刷新时创建一个新的数据库条目,无需表单提交
我是 Rails 和 mongodb 的新手,并且有一个简单的表单尝试创建类别树。
每当我刷新页面时,就会将一个新条目放入数据库中。 我没有点击“提交”按钮,只是刷新页面。
表单看起来像这样,
<%= form_for Activity.create do |f| -%> <%= f.text_field :activity_name % > <%= f.submt "add action" %< <% end %>
我的模型是
class Activity include MongoMapper::Document key :activity_name, :type => String key :parent, :type => ObjectId key :acnestors, Array timestamps! end
它有一个活动条目
map.activity '/activity/:activity_id', :controller => 'activities', :action => 'show'
我的路线,尽管我从表单调用创建,但我的控制器中的创建函数是空的,但 。 该表单通过渲染包含在显示页面中,但这并不重要。
知道为什么页面刷新会充当表单提交吗?
I'm new to rails and mongodb, and have a simple form attempting to create a category tree.
Whenever I refresh the page, a new entry is put into the database.
I'm not clicking the 'submit' button, just page refresh.
The form looks like this
<%= form_for Activity.create do |f| -%> <%= f.text_field :activity_name % > <%= f.submt "add action" %< <% end %>
my model is
class Activity include MongoMapper::Document key :activity_name, :type => String key :parent, :type => ObjectId key :acnestors, Array timestamps! end
my routes has a single entry for activity
map.activity '/activity/:activity_id', :controller => 'activities', :action => 'show'
though I call create from the form, my create function in my controller is empty.
The form is included in the show page via render, but that shouldn't matter.
Any idea why a page refresh would act as a form submit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在表单助手中调用
Activity.create
。这将在每次加载页面时创建(在 Rails 意义上,这也会将其保存到数据库)一个新对象。You are calling
Activity.create
in your form helper. This is going to create (in the Rails sense, which will also save it to the db) a new object every time you load the page.