On Tue, Feb 7, 2012 at 8:35 AM, Soichi Ishida wrote:

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.
Hi!

Maybe your Ajax Request is being submitted twice.


Thanks in advance.

soichi

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 7, '12 at 10:35a
activeFeb 8, '12 at 12:29a
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Soichi Ishida: 2 posts Everaldo Gomes: 1 post

People

Translate

site design / logo © 2021 Grokbase