【问题标题】:Rails 3 :remote => true & . creating duplicated calls to createRails 3 :remote => true & 。创建重复调用创建
【发布时间】:2013-09-13 06:40:22
【问题描述】:

在搜索了一段时间并将其缩小到一个特定的问题之后,我决定最终注册并向各位好心人寻求帮助。如下:

我创建了一个表单:

= form_for @note, :remote => true do |f|

在控制器内部使用 .build(或 .create):

class NotesController < ApplicationController
  before_filter :authenticate_user!
  respond_to :html, :js

  def create
    @note = current_user.notes.build(params[:note])
    if @note.save
      respond_with @note, :location => root_url
    end
  end

它会循环两次创建块。

如果我使用Note.new 或删除:remote =&gt; true,那么一切正常,但两者的结合会破坏一切。笔记创建在控制台中工作正常,它不会在任何地方抛出任何错误,所以我被卡住了。

有人有什么建议吗?

【问题讨论】:

  • 你的意思是浏览器正在发送两个 Ajax 调用来创建方法?
  • 请检查 javascript 控制台,在远程 => true 是否发送两个请求?

标签: javascript ruby-on-rails ajax forms


【解决方案1】:

如果你使用remote true,那么你需要创建一个create.js.erb,因为默认情况下它会尝试渲染js.erb

【讨论】:

    【解决方案2】:

    据我了解,您在新操作中创建了一个空白对象,并在保存创建操作之前使用参数值填充该对象。试试这个:

    class NotesController < ApplicationController
      before_filter :authenticate_user!
      respond_to :html, :js
    
      def new
        @note = Note.new
      end
    
      def create
        @note = current_user.notes.new(params[:note])
        respond_to do |format|
          if @note.save
           format.js
          end
        end
      end
    
    end
    

    【讨论】:

      猜你喜欢
      • 2012-05-14
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      相关资源
      最近更新 更多