Noz
Noz

Reputation: 6346

Rails 3 js.erb is never called

I'm trying to keep the user on the same page and execute some Javascript after an Update is performed..

However my js.erb never seems to get called and instead I get redirected to the route "http://localhost:3000/upload_images/id" and my firebug console produces...

"NetworkError: 406 Not Acceptable -http://localhost:3000/upload_images/id"

Update Action

    def update  
    @upload_image = UploadImage.find(params[:id])
    #If this image is set as the preview restore all associated images to default
    if params[:upload_image][:preview] == "1"
      other_uploads = @upload_image.upload.upload_images.where("not upload_images.id = ?", @upload_image.id)
      other_uploads.each do |upload_image|
        upload_image.preview = "0"
        upload_image.save
      end
    end      
    respond_to do |format|    
      if @upload_image.update_attributes(params[:upload_image])
        #keep the user on current page and call some Javascript
        format.js{render :template => 'update.js.erb'}
      else
        format.html { render action: "edit" }
        format.json { render json: @upload_image.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end

Update.js.erb

alert("hello world!");

Edit form

<%=form_for(@upload_image) do |f| %>
   ....
<% end %>

It's not important for me to see the live updates, I don't mind if the user has to refresh to see them. I just want to keep the page state as is (with the exception of some changes made by JQuery) and make a post to the database.

Upvotes: 0

Views: 200

Answers (1)

Matt Glover
Matt Glover

Reputation: 1347

You probably need to switch to a remote form:

<%= form_for(@upload_image, :remote => true) do |f| %>

Otherwise your form will attempt an HTTP POST that expects an HTTP response.

Upvotes: 2

Related Questions