0

In my rails3 application, a user can add an item to a list by clicking an "add item" link. Clicking this link makes AJAX call to grab the new_item form. That part I have working. The problem is that when I submit the new_item form, it doesn't seem to be recognized as a "remote" form even though I've specified :remote => true. It's clear that I need to re-initialize the UJS or something after the new_item form is returned from the server and injected into the page, but I don't know how?

Here's my code:

_new.html.haml (new item form)

= 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

When I submit the new_item form I get an error in the JS console which looks like this... I have a create.js.erb template which is just blank, but it's not looking for this and clearly not recognizing the request as an ajax request. How do i get the form returned from the server to be 'ujs' enabled again for lack of a better word?

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"

Edit - here's the updated controller code that allows the alert to be triggered.

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
4

1 回答 1

1

Plain:remote => true发出一个接受application/javascript, */*(或类似的东西)的请求。但是您的控制器不响应 javascript,因此它会尝试呈现 html 模板。

尝试强制json形式:

= form_for [@list, @item], :remote => true, :html => {:class => 'form-inline', 'data-type' => 'json'} do |f|
于 2013-03-04T16:18:37.893 回答