AJAX-Call后双重保存

时间:2013-08-03 10:26:07

标签: javascript ruby-on-rails ruby ajax coffeescript

通过使用AJAX和Rails保存记录,我遇到了一个奇怪的问题。我想通过使用带有jQuery Sortable(http://jqueryui.com/sortable/#connect-lists)的拖放来保存记录。节约工作正常...差不多。应该通过将元素从一个列表拖到另一个列表来保存的相同记录被保存两次:(我不知道为什么。也许有人可以帮助我...

查看/ dev_languages / new.html.haml

=title_header(t('.your_qualifications'))
#dev_languages
  .container_12
    #dev_language_type_btns
      %span#application_btn= 'Application'
      %span#web_btn= 'Web'
      %span#mobile_btn= 'Mobile'

  .container_12
    .grid_6
      #application_dev_languages
        =render @application_dev_languages
      #web_dev_languages
        =render @web_dev_languages
      #mobile_dev_languages
        =render @mobile_dev_languages
    .grid_6
      %h3= "Skill Wall"
      %ul#skill_wall.connectedSortable
        %li= "blubb"
        %li= "bla"

assets / javascripts / dev_languages.js.coffee

$("#languages, #skill_wall").sortable(
  connectWith: ".connectedSortable"
  update: ->
    alert "www"
    $.ajax
      url: "/has_dev_languages"
      type: "post"
      #processData: false
      data:
        creator_id: 1
        creator_type: "Expert"
        application_type_id: 1
        dev_language_id: 1
  ).disableSelection()

controllers / has_dev_languages_controller.rb

class HasDevLanguagesController < ApplicationController
  before_filter :authenticate_user!

  def create
    @has_dev_languages = HasDevLanguage.create(
        :creator_id       => params[:creator_id],
        :creator_type     => params[:creator_type],
        :application_type_id => params[:application_type_id],
        :dev_language_id  => params[:dev_language_id]
    )
    respond_to do |wants|
      wants.js
    end
  end

控制器/ dev_languages_controller.rb

class DevLanguagesController < ApplicationController
  before_filter :authenticate_user!
  before_filter :find_base_data

  private

    def find_base_data
      @application_dev_languages  = DevLanguage.where(:application_type_id => '1')
      @web_dev_languages          = DevLanguage.where(:application_type_id => '2')
      @mobile_dev_languages       = DevLanguage.where(:application_type_id => '3')
    end

查看/ has_dev_languages / create.js.erb

window.location.reload();

服务器-登录

Started POST "/has_dev_languages" for 127.0.0.1 at 2013-08-03 12:18:27 +0200
Processing by HasDevLanguagesController#create as */*
  Parameters: {"creator_id"=>"1", "creator_type"=>"Expert", "application_type_id"=>"1", "dev_language_id"=>"1"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `has_dev_languages` (`application_type_id`, `created_at`, `creator_id`, `creator_type`, `dev_language_id`, `job_posting_id`, `updated_at`) VALUES (1, '2013-08-03 10:18:27', 1, 'Expert', 1, NULL, '2013-08-03 10:18:27')
   (1.3ms)  COMMIT
  Rendered has_dev_languages/create.js.erb (0.0ms)
Completed 200 OK in 10ms (Views: 4.6ms | ActiveRecord: 2.0ms)


Started POST "/has_dev_languages" for 127.0.0.1 at 2013-08-03 12:18:27 +0200
Processing by HasDevLanguagesController#create as */*
  Parameters: {"creator_id"=>"1", "creator_type"=>"Expert", "application_type_id"=>"1", "dev_language_id"=>"1"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `has_dev_languages` (`application_type_id`, `created_at`, `creator_id`, `creator_type`, `dev_language_id`, `job_posting_id`, `updated_at`) VALUES (1, '2013-08-03 10:18:27', 1, 'Expert', 1, NULL, '2013-08-03 10:18:27')
   (1.6ms)  COMMIT
  Rendered has_dev_languages/create.js.erb (0.0ms)
Completed 200 OK in 10ms (Views: 4.5ms | ActiveRecord: 2.3ms)


Started GET "/has_dev_languages/__meta_request/d7377832adbf2d9a0460a0a4426b5d2f.json" for 127.0.0.1 at 2013-08-03 12:18:28 +0200


Started GET "/de/dev_languages/new" for 127.0.0.1 at 2013-08-03 12:18:29 +0200
Processing by DevLanguagesController#new as HTML
  Parameters: {"locale"=>"de"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  DevLanguage Load (0.3ms)  SELECT `dev_languages`.* FROM `dev_languages` WHERE `dev_languages`.`application_type_id` = 1
  Rendered dev_languages/_dev_language.html.haml (0.1ms)
  DevLanguage Load (0.2ms)  SELECT `dev_languages`.* FROM `dev_languages` WHERE `dev_languages`.`application_type_id` = 2
  Rendered dev_languages/_dev_language.html.haml (0.1ms)
  DevLanguage Load (0.1ms)  SELECT `dev_languages`.* FROM `dev_languages` WHERE `dev_languages`.`application_type_id` = 3
  Rendered dev_languages/_dev_language.html.haml (0.1ms)
  Rendered dev_languages/new.html.haml within layouts/application (6.3ms)
  Rendered shared/_flashes.html.haml (0.1ms)
  Rendered shared/_header.html.haml (1.1ms)
  Rendered shared/_footer.html.haml (0.4ms)
  Rendered devise/shared/_links.erb (0.6ms)
  Rendered modals/forms/_login_expert_form.html.haml (12.0ms)
  Rendered modals/_login_expert.html.haml (12.8ms)
Completed 200 OK in 205ms (Views: 202.5ms | ActiveRecord: 1.0ms)


Started GET "/has_dev_languages/__meta_request/a9d0157927d47455f5af2be3593404c5.json" for 127.0.0.1 at 2013-08-03 12:18:29 +0200


Started GET "/de/dev_languages/new/__meta_request/fa0990fcfbf1ee313d620ed95e7aa3d4.json" for 127.0.0.1 at 2013-08-03 12:18:29 +0200

请帮帮我。这个问题我在这里待了3天! :( :( :(

谢谢!

0 个答案:

没有答案