Question

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
Was it helpful?

Solution

Plain :remote => true makes a request that accepts application/javascript, */* (or something similar). But your controller does not respond to javascript, so it tries to render html template.

Try to force json in the form:

= form_for [@list, @item], :remote => true, :html => {:class => 'form-inline', 'data-type' => 'json'} do |f|
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top