简体   繁体   English

在Rails 4中提交AJAX请求时遇到问题

[英]Problems submitting AJAX request in Rails 4

I've been following http://blog.markhorgan.com/?p=522 as a guide to update an image in a form with an ajax callback. 我一直在遵循http://blog.markhorgan.com/?p=522作为使用ajax回调形式更新图像的指南。 Image saves fine but I want to do some clever ajax so the page doesn't refresh. 图片保存得很好,但是我想做一些巧妙的ajax,所以页面不会刷新。

Here's my code: 这是我的代码:

edit.html.haml: edit.html.haml:

  #promo-image
     = render partial: 'promo_image' 

_promo_image.html.haml: _promo_image.html.haml:

= form_for( @property, remote: true) do |f|
  = f.file_field :promo_image, :pattern => "^.+?\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$", :id => 'promo-image-upload'
  = f.submit 'Update'
= image_tag @property.promo_image.url(:medium)

properties_controller.rb properties_controller.rb

def update
 @property = Property.find(params[:id])

 if @property.update(property_params)
   format.js
 else
   render 'edit'
 end
end

update.js.haml: update.js.haml:

$("#promo-image").html("#{escape_javascript(render partial: 'promo_image',)}"); $(“#promo-image”)。html(“#{escape_javascript(render部分:'promo_image',)}”));

With the code outlined above I get error pointing to the format.js line: 使用上面概述的代码,我得到了指向format.js行的错误:

ArgumentError in PropertiesController#update too few arguments PropertiesController#update中的ArgumentError更新参数太少

Can anyone see where I'm going wrong or perhaps point me in the right direction? 谁能看到我要出问题的地方,或者可以向我指出正确的方向?

Many thanks! 非常感谢! Steve 史蒂夫

UPDATE UPDATE

Just to be clear, I want to be able to update JUST the Div stated here: 只是为了清楚起见,我希望能够仅更新此处所述的Div:

update.js.haml: update.js.haml:

$("#promo-image").html("#{escape_javascript(render partial: 'promo_image',)}"); $(“#promo-image”)。html(“#{escape_javascript(render部分:'promo_image',)}”));

This code works, but refreshes the whole page: 这段代码有效,但是刷新了整个页面:

 respond_to do |format| format.html { redirect_to edit_property_path(@property) } format.js end 

FURTHER UPDATE 进一步更新

Just to be clear on my motives, I want to be able to update an element on the edit page, and not be redirected to a different one, eg show or index. 为了明确我的动机,我希望能够更新编辑页面上的元素,而不是将其重定向到其他元素,例如show或index。 This is for UI reasons. 这是出于UI原因。 The guide above talks about the exact same thing. 上面的指南讨论了完全相同的事情。

FINAL UPDATE 最后更新

The issue is because I'm using a file upload, this can't be achieved via ajax. 问题是因为我正在使用文件上传,所以无法通过ajax实现。 For those in a similar situation see here: Rails form_for with file_field and remote => true and format => :js 对于处于类似情况的用户,请参见此处: Rails form_for与file_field和remote => true和format =>:js

A solution could lay here, and I will investigate this: https://github.com/JangoSteve/remotipart 解决方案可以放在这里,我将对此进行调查: https : //github.com/JangoSteve/remotipart

Thanks to everyone for helping me work out the error of my ways! 感谢大家帮助我解决自己的错误方式!

Regarding your first update, you said that this code works, but refreshes the page: 关于您的第一次更新,您说此代码有效,但是刷新了页面:

respond_to do |format|
  format.html { redirect_to edit_property_path(@property) } 
  format.js
end

If that is the case, that means the incoming request is an html request, rather than an AJAX request. 如果是这种情况,则意味着传入的请求是html请求,而不是AJAX请求。 So the format.html block runs and redirects the browser to the same page, which now has the updated image. 因此,format.html块将运行,并将浏览器重定向到同一页面,该页面现在具有更新的图像。

What you need to do is figure out why the page is not sending the request as AJAX. 您需要做的是弄清楚为什么页面没有以AJAX格式发送请求。 You can see the request format if you look at the terminal output (if running locally). 如果查看终端输出(如果在本地运行),则可以看到请求格式。 It will say something like: 它会说类似:

Processing by ControllerName#action as [format]

Format needs to be JS in order for the format.js to render update.js.haml. 格式必须为JS,以便format.js呈现update.js.haml。

UPDATE: 更新:

Now that you mention it, the issue is indeed the file_upload field. 既然您已提到它,问题确实是file_upload字段。 Uploading files with AJAX is actually not possible with the Forms Helper. 使用Forms Helper实际上无法使用AJAX上传文件。 See the docs : 文档

Unlike other forms making an asynchronous file upload form is not as simple as providing form_for with remote: true. 与其他表单不同,制作异步文件上载表单并不像为form_for提供remote:true那样简单。 With an Ajax form the serialization is done by JavaScript running inside the browser and since JavaScript cannot read files from your hard drive the file cannot be uploaded. 使用Ajax格式时,序列化由运行在浏览器中的JavaScript完成,并且由于JavaScript无法从硬盘驱动器读取文件,因此无法上传文件。 The most common workaround is to use an invisible iframe that serves as the target for the form submission. 最常见的解决方法是使用不可见的iframe作为表单提交的目标。

I did a quick search on Google and found the remotipart gem , which seems to specialize in doing this. 我在Google上进行了快速搜索,发现了remotipart gem ,它似乎专门从事此工作。 I don't have any experience with it though, so you're on your own from here on. 不过我没有任何经验,因此从现在开始您就是一个人。 :) :)

Try changing your update action to 尝试将update操作更改为

def update
  @property = Property.find(params[:id])

 if @property.update(property_params)

    respond_to do |format|
      format.html { redirect_to properties_path } 
      format.js
    end
  else
    render 'edit'
  end
end

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM