0

In rails 3

While using remote method form submit, it affects the database twice with the same form values.

controller file:

 @user_message = UserMessage.new(params[:user_message])
 if  @user_message.save
   render :update
 else
   render :nothing => true
 end

view file:

<%= form_tag ('/feedback/user_message'), :method =>'post',  :remote=> true, :id=>'user_message' do%>
      <%= hidden_field_tag 'user_message[user_id]', @user.user_id %>
      <h2><%= @question %></h2>
      <%= text_area_tag 'user_message[msg]',"",  :size=>"40x5" %>
      <%= submit_tag  "Submit"%>
  <% end %>

When I hit the submit button it creates two records on the table. Why?

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
rascaL raajA
  • 231
  • 1
  • 10

2 Answers2

0

The problem most likely related to asset pipeline

You should precompiled the asset pipeline before.

This will create two copy of rails.js, one in your assets and one in application.js

This is a bug or gotcha in rails 3 See here http://www.ruby.code-experiments.com/blog/2011/10/another-gotcha-with-the-rails-31-asset-pipeline-or-why-are-my-jquery-ujs-ajax-requests-triggered-twi.html

Hope this help

Alan Chan
  • 168
  • 8
0

There may be couple of things that might be causing this

may be the ujs file is included twice may may be like

//= require jquery_ujs // expected to load from the rails-jquery gem
//= require_tree .     // if any file is present in assets directory hierarchy it will be loaded

Or just a bug taking your sleep away

https://github.com/rails/jquery-ujs/issues/208

check out more solutions

Jquery Rails 3... form submits twice... deletes twice... help

Rails 3.1 remote requests submitting twice

This may also be related to the understanding of asset pipeline when and how use pecompiled assets.

I always use this config in development mode

config.server_static_assets = false

this forces the app to call the assets from app assets

and use precompiled assets from public in production mode

Community
  • 1
  • 1
Pritesh Jain
  • 9,106
  • 4
  • 37
  • 51