1

通过使用 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"

资产/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()

控制器/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天!:( :( :(

谢谢!

4

0 回答 0