save method (create action) saves twice

Rails 3.1.3

I have changed a regular scaffold action a bit so that it can save using ajax. All I needed to do was adding ":remote => true" in

<div id="script_new">   <%= form_for script, :remote => true do |f| %> <!-- HERE!!! -->     <%= f.hidden_field :video_id %>   <%= f.text_field :text %>   <%= f.submit "save" %>   <% end %> </div>

then even though the whole page does not change itself, I can see the data saved by reloading the page. Namely, the data is in fact saved in the database ( Eventually I will modify it so that it will reflect in the page using javascript + ajax).

But the problem is that a single 'save' action (clicking the "save" button) seems to save the same data twice. Why is that???

After executing the saving method, the following messages appear consecutively in the command console where the rails server is up. They seem similar to each other, but two separate SQL actions.

Started POST "/scripts" for 127.0.0.1 at 2012-02-07 19:17:53 +0900   Processing by ScriptsController#create as JS   Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx+gv5Cr9GjC0UpqfH89qgnRii4=", "script"=>{"video_id"=>"1", "startp"=>"0", "endp"=>"20", "text"=>"This is a test!"}, "commit"=>"save"}   SQL (0.7ms) INSERT INTO "scripts" ("created_at", "endp", "startp", "text", "updated_at", "video_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Tue, 07 Feb 2012 10:17:53 UTC +00:00], ["endp", 20], ["startp", 0], ["text", "This is a test!"], ["updated_at", Tue, 07 Feb 2012 10:17:53 UTC +00:00], ["video_id", 1]] Completed 201 Created in 14ms (Views: 2.3ms | ActiveRecord: 0.9ms)

Started POST "/scripts" for 127.0.0.1 at 2012-02-07 19:17:53 +0900   Processing by ScriptsController#create as JS   Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx+gv5Cr9GjC0UpqfH89qgnRii4=", "script"=>{"video_id"=>"1", "startp"=>"0", "endp"=>"20", "text"=>"This is a test!"}}   SQL (0.6ms) INSERT INTO "scripts" ("created_at", "endp", "startp", "text", "updated_at", "video_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Tue, 07 Feb 2012 10:17:53 UTC +00:00], ["endp", 20], ["startp", 0], ["text", "This is a test!"], ["updated_at", Tue, 07 Feb 2012 10:17:53 UTC +00:00], ["video_id", 1]] Completed 201 Created in 14ms (Views: 2.4ms | ActiveRecord: 0.9ms)

To be honest, I have no idea which part of my code to show you in order to gain some help to resolve this because I have not changed much from the regular scaffold generation.

But this is the create action in scripts_controller.rb. It is to throw json data so that Ajax will catch it.

  def create     @script = Script.new(params[:script])     respond_to do |format|       if @script.save         format.json { render json: @script, status: :created, location: @script }       else         format.html { render action: "new" }         format.json { render json: @script.errors, status: :unprocessable_entity }       end     end   end

And script.rb

class Script < ActiveRecord::Base   belongs_to :video   has_many :users end

If the information I provide is not enough, please indicate which part of my code is ought be disclosed.

Thanks in advance.

soichi

Rails 3.1.3

I have changed a regular scaffold action a bit so that it can save using

ajax.

All I needed to do was adding “:remote => true” in

<%= form_for script, :remote => true do |f| %>

<%= f.hidden_field :video_id %>

<%= f.text_field :text %>

<%= f.submit “save” %>

<% end %>

then even though the whole page does not change itself, I can see the

data saved by reloading the page. Namely, the data is in fact saved in

the database ( Eventually I will modify it so that it will reflect in

the page using javascript + ajax).

But the problem is that a single ‘save’ action (clicking the “save”

button) seems to save the same data twice. Why is that???

After executing the saving method, the following messages appear

consecutively in the command console where the rails server is up. They

seem similar to each other, but two separate SQL actions.

Started POST “/scripts” for 127.0.0.1 at 2012-02-07 19:17:53 +0900

Processing by ScriptsController#create as JS

Parameters: {“utf8”=>“✓”,

“authenticity_token”=>“xxx+gv5Cr9GjC0UpqfH89qgnRii4=”,

“script”=>{“video_id”=>“1”, “startp”=>“0”, “endp”=>“20”, “text”=>"This

is a test!"}, “commit”=>“save”}

SQL (0.7ms) INSERT INTO “scripts” (“created_at”, “endp”, “startp”,

“text”, “updated_at”, “video_id”) VALUES (?, ?, ?, ?, ?, ?)

[[“created_at”, Tue, 07 Feb 2012 10:17:53 UTC +00:00], [“endp”, 20],

[“startp”, 0], [“text”, “This is a test!”], [“updated_at”, Tue, 07 Feb

2012 10:17:53 UTC +00:00], [“video_id”, 1]]

Completed 201 Created in 14ms (Views: 2.3ms | ActiveRecord: 0.9ms)

Started POST “/scripts” for 127.0.0.1 at 2012-02-07 19:17:53 +0900

Processing by ScriptsController#create as JS

Parameters: {“utf8”=>“✓”,

“authenticity_token”=>“xxx+gv5Cr9GjC0UpqfH89qgnRii4=”,

“script”=>{“video_id”=>“1”, “startp”=>“0”, “endp”=>“20”, “text”=>"This

is a test!"}}

SQL (0.6ms) INSERT INTO “scripts” (“created_at”, “endp”, “startp”,

“text”, “updated_at”, “video_id”) VALUES (?, ?, ?, ?, ?, ?)

[[“created_at”, Tue, 07 Feb 2012 10:17:53 UTC +00:00], [“endp”, 20],

[“startp”, 0], [“text”, “This is a test!”], [“updated_at”, Tue, 07 Feb

2012 10:17:53 UTC +00:00], [“video_id”, 1]]

Completed 201 Created in 14ms (Views: 2.4ms | ActiveRecord: 0.9ms)

To be honest, I have no idea which part of my code to show you in order

to gain some help to resolve this because I have not changed much from

the regular scaffold generation.

But this is the create action in scripts_controller.rb. It is to throw

json data so that Ajax will catch it.

def create

@script = Script.new(params[:script])

respond_to do |format|

  if @script.save

    format.json { render json: @script, status: :created, location:

@script }

  else

    format.html { render action: "new" }

    format.json { render json: @script.errors, status:

:unprocessable_entity }

  end

end

end

And script.rb

class Script < ActiveRecord::Base

belongs_to :video

has_many :users

end

If the information I provide is not enough, please indicate which part

of my code is ought be disclosed.

Hi!

Maybe your Ajax Request is being submitted twice.

Thanks! You were right. My app was reading a JavaScript Library TWICE...

soichi