0

I have a simple remote form logic in my Rails app to respond to new, create, edit and update actions of my controller with AJAX. My form_for...remote => true responds with format.js, but not to my successful create-actions. I use the same form-partial for all, my update-action responds with .js and my create-action responds with .js only if the saving was not successful. If I submit a valid form for a new item, teh app throws an ActionController::UnknownFormat error. How can this only concern the successful create-action?

I have the <%= javascript_include_tag 'application' %> and <%= csrf_meta_tags %> included and below is my code:

form-partial:

# variable remote_form is true
<%= form_for(shop_item, :remote => remote_form, :authenticity_token => true) do |f| %>
...
<% end %>

controller:

def create
  @shop_item = ShopItem.new(shop_item_params)
  if @shop_item.save
    respond_to do |format|
      format.js do
        flash.now[:success] = "New Item #{@shop_item.title} created"
      end
    end
  else
    respond_to do |format|
      format.js { render 'new' }
    end
  end
end

def edit
  @shop_item = ShopItem.find(params[:id])
  respond_to do |format|
    format.js
  end
end

def update
  @shop_item = ShopItem.find(params[:id])
  if @shop_item.update_attributes(shop_item_params)
    @shop_item.reload
    respond_to do |format|
      format.js do
        flash.now[:success] = "Updated #{@shop_item.title}"
      end
    end
  else
    respond_to do |format|
      format.js { render 'edit' }
    end
  end
end

create.js.erb:

if ($('#flashes').length > 0) {
  $('#flashes').replaceWith('<%= j(render :partial => "shared/flashes") %>');
} else {
  $('#session').append('<%= j(render :partial => "shared/flashes") %>');
}

$('#shop-items').prepend('<%= j(render @shop_item) %>');

server:

ActionController::UnknownFormat (ActionController::UnknownFormat):    
app/controllers/shop_items_controller.rb:20:in `create'
...

Thanks!

R4ttlesnake
  • 1,661
  • 3
  • 18
  • 28

0 Answers0