Rails form_for 在刷新时创建一个新的数据库条目,无需表单提交

发布于 2024-09-14 06:27:07 字数 717 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈甜 2024-09-21 06:27:07

您正在表单助手中调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文