So, I play with CoffeeScript, Rails 3.1 is all good. I have a resource with all the usual route indexes, showing, creating, editing, updating, destroying.
There is a form in the index view that uses :remote => true as follows:
<%= form_for @todo, :remote => true do |f| %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
In the controller to create, I have the following:
def create @todo = Todo.new(params[:todo]) respond_to do |format| if @todo.save format.html { redirect_to @todo, notice: 'Todo was successfully created.' } format.json { render json: @todo, status: :created, location: @todo } format.js {render json: @todo } else format.html { render action: "new" } format.json { render json: @todo.errors, status: :unprocessable_entity } end end end
I try not to use .js.erb views as I would rather handle the returned JSON and do all the fancy additions to the todo list and so on. (It just feels cleaner for me).
In my todos.js.coffee, I used the following:
$(document).ready -> $("#new_todo") .bind "ajax:success", (event, data) -> alert("Ajax SUCCESS!!!")
(Yes, just typing to open a warning window doesnβt work) I tried downloading, but just canβt trigger this event. The request succeeds and a new todo is added.
Any help with this would be greatly appreciated. Thanks