开发者

Problem using 'responds_to_parent' with an iframe and AJAX to upload a file

开发者 https://www.devze.com 2023-02-05 10:24 出处:网络
I followed instructions from the \"AJAX file uploads in Rails using attachment_fu and responds_to_parent\" article. In my case I am using Ruby on Rails 3 and Paperclip.

I followed instructions from the "AJAX file uploads in Rails using attachment_fu and responds_to_parent" article. In my case I am using Ruby on Rails 3 and Paperclip.

What I made is the following:


I have installed the 'respons_to_parent' plugin running this command in the Terminal:

rails plugin install git://github.com/itkin/respond_to_parent.git

After installing, I restart Apache.


In my view I have:

<%= form_for(@user, :html => { :multipart => true, :target => 'upload_frame' }) do |f| %>
  <%= f.file_field :avatar %>
  <%= f.submit "Submit" %>
<% end %>

<div id="test">Here is a test</div>
<div id="stuff">Here is some stuff</div>
<iframe id='upload_frame' name="upload_frame" style="width:1px;height:1px;border:0px" ></iframe>

In my controller I have

  def action
    respond_to do |format|
      ...
      format.js {
      开发者_开发技巧  responds_to_parent do
          render :update do |page|
            page.replace_html :test, "This is the resulting test"
            page << "alert($('stuff').innerHTML)"
          end
        end
      end
    end
  end

Trying to submit the form, all about the file uploading works (Paperclip handle correclty files, ...) and in the log file there aren't errors.

The only thing that isn't working is the AJAX part. In the example

page.replace_html :test, "This is the resulting test"
page << "alert($('stuff').innerHTML)"

don't update the page (BTW: where these should have effect? in the "main view" or in the "iframe view"?). It doesn't work also if I try to delete the 'respons_to_parent' statement or if I put them in the 'action.js.rjs' file.

Where I am wrong?


SOLVED

I had forgotten to add :url => users_account_path +. "js". So the view become:

<%= form_for(@user, :url => users_account_path +. "js", :html => { :multipart => true, :target => 'upload_frame' }) do |f| %>
0

精彩评论

暂无评论...
验证码 换一张
取 消