Javascript Rails 3:remote=>;真的。创建重复调用以创建
在搜索了一段时间并将其缩小到一个特定的问题之后,我决定最终注册,并向你们这些好人寻求帮助。下面是: 我创建一个表单时使用:Javascript Rails 3:remote=>;真的。创建重复调用以创建,javascript,ruby-on-rails,ajax,forms,Javascript,Ruby On Rails,Ajax,Forms,在搜索了一段时间并将其缩小到一个特定的问题之后,我决定最终注册,并向你们这些好人寻求帮助。下面是: 我创建一个表单时使用: = form_for @note, :remote => true do |f| class NotesController < ApplicationController before_filter :authenticate_user! respond_to :html, :js def create @note = current_
= form_for @note, :remote => true do |f|
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
在控制器内部使用.build(或.create):
= form_for @note, :remote => true do |f|
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
class NotesControllerroot\u url响应
结束
结束
并在创建块中循环两次。
如果我使用Note.new
或remove:remote=>true
则一切正常,但两者的结合破坏了一切。注释创建在控制台中运行良好,它不会在任何地方抛出任何错误,所以我被卡住了
有人有什么建议吗?如果您使用的是remote true,那么您需要创建一个create.js.erb,因为默认情况下,它将尝试渲染js.erb据我所知,您在新操作中创建一个空白对象,并在保存到创建操作之前用参数值填充该对象。试试这个:
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
class NotesController
您的意思是浏览器正在发送两个Ajax调用来创建方法?请检查javascript控制台,on remote=>true是否发送两个请求?