views:

18

answers:

1

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?

A: 

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.

jdl
Thanks jdl, i ended up with form_for :activity, :url => {:action => "new" } do |f|I hope that is right, it seems to have solved the problem.
pedalpete
The action should be create. Your new action should be used to set up the object that you plan to populate in your form (same for the edit action, except that it would be grabbing an existing record instead of instantiating a new one).
jdl