0

我试图让用户在同一页面上并在执行更新后执行一些 Javascript..

然而,我的 js.erb 似乎从未被调用,而是被重定向到路由“http://localhost:3000/upload_images/id”,而我的萤火虫控制台产生了......

"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

更新.js.erb

alert("hello world!");

编辑表格

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

看到实时更新对我来说并不重要,我不介意用户是否必须刷新才能看到它们。我只想保持页面状态不变(JQuery 所做的一些更改除外)并发布到数据库。

4

1 回答 1

2

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

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

否则,您的表单将尝试期望 HTTP 响应的 HTTP POST。

于 2012-07-04T21:14:55.630 回答