永远不会调用Rails 3 js.erb

时间:2012-07-04 21:06:28

标签: jquery ajax ruby-on-rails-3

我正在尝试让用户保持在同一页面上并在执行更新后执行一些Javascript。

然而,我的js.erb似乎永远不会被调用,而是被重定向到路由“http:// localhost:3000 / upload_images / id”并且我的firebug控制台产生......

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

更新行动

    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!");

修改表单

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

我看到实时更新并不重要,我不介意用户是否需要刷新才能看到它们。我只想保持页面状态不变(除了JQuery做出的一些更改)并向数据库发帖。

1 个答案:

答案 0 :(得分:2)

您可能需要切换到远程表单:

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

否则,您的表单将尝试一个需要HTTP响应的HTTP POST。