2013-03-04 56 views
0

在我的rails3应用程序中,用户可以通过单击“添加项目”链接将项目添加到列表。点击这个链接可以让AJAX调用抓取new_item表单。那部分我有工作。问题是,当我提交new_item表单时,即使指定了:remote => true,它似乎也不会被识别为“远程”表单。很明显,我需要在从服务器返回new_item表单并将其注入页面之后重新初始化UJS或其他内容,但我不知道如何?从AJAX请求返回的表单在提交时不被识别为'remote'

这里是我的代码:

_new.html.haml(新项目的形式)

= form_for [@list, @item], :remote => true, :html => {:class => 'form-inline'} do |f| 
    .input-append 
    = f.text_field "name", :class => 'input-large', :placeholder => "Add item to this list" 
    %button#picture.btn 
     %span.icon-camera 
    %button#link.btn{:style => "font-size: 10px;"} 
     http:// 
    #link-field.field.margin.hidden-field-margin 
    = f.text_field "link", :placeholder => "http://www.link.com", :style => "width: 325px" 
    #picture-field.field.margin.hidden-field-margin 
    = f.file_field "picture" 
    = f.hidden_field "picture_cache" 
    .clearfix 
    = f.submit "Add Item", :class => 'btn', :id => "add-item" 

lists.js.coffee

# this retrieves the new item form and places it in-line in the page   
$("#new_list") 
    .bind "ajax:success", (evt, xhr, settings) -> 
     $("#list-item").html(xhr) 

# when the new item form (returned from the server) is submitted here, it doesn't appear to be submitted via AJAX 
$("#new_item #add-item") 
    .live "ajax:success", (evt, xhr, settings) -> 
     alert "this request worked" 

items_controller.rb

class ItemsController < ApplicationController 
    respond_to :html, :xml, :json 
    def create 
    @list = List.find(params[:list_id]) 
    @item = @list.items.create!(params[:item]) 
    if @item.save 
     respond_with do |f| 
     f.html do 
      if request.xhr? 
      f.json { render :json => {:result => "ok"}} 
      else 
      redirect_to @list, :notice => "Item was successfully created." 
      end 
     end 
     end 
    end 
    end 
end 

当我提交new_item表单时,我在JS控制台中看到一个错误,看起来像这样...我有一个只是空白的create.js.erb模板,但它不寻找这个,显然不是将该请求识别为ajax请求。我如何从服务器返回的表单被'ujs'再次启用,因为缺乏更好的单词?

Template is missing 

Missing template items/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in: * "/Users/aressidi/Projects/intralist/app/views" * "/Users/aressidi/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/app/views" 

编辑 - 这里是更新后的控制器代码,允许将触发警报。

class ItemsController < ApplicationController 
    respond_to :html, :xml, :json 
    def create 
    @list = List.find(params[:list_id]) 
    @item = @list.items.create!(params[:item]) 
    if @item.save 
     respond_with do |f| 
     f.html do 
      if request.xhr? 
      render :json => {:result => "ok"} 
      else 
      redirect_to @list 
      end 
     end 
     end 
    end 
    end 
end 
+0

为什么不试试'的respond_to:js'? – 2013-03-04 16:25:24

+0

这工作,感谢Manoj – aressidi 2013-03-04 18:21:59

+0

不客气:) – 2013-03-04 18:23:36

回答

1

:remote => true平原使得接受application/javascript, */*(或类似的东西)的请求。但你的控制器不响应JavaScript,所以它会尝试呈现html模板。

试图迫使json形式:

= form_for [@list, @item], :remote => true, :html => {:class => 'form-inline', 'data-type' => 'json'} do |f| 
+0

感谢Dimuch,我不知道那里发生了什么,并了解控制器试图呈现HTML帮助我磨练解决方案。我上面发布了更新。只需要对控制器代码进行一些更改即可使警报工作。 – aressidi 2013-03-04 18:23:31