mrudult
mrudult

Reputation: 2570

Rails ajax get data from s3 not working

I have a form field as:

<%= select_tag "select", options_for_select(@folders),:include_blank => true %>  

I'm doing a ajax call as (from new.html.erb):

$(document).ready(function(){
    $("#select").change(function(){
        $.ajax({
          type: "PUT",
          data: {folder: $(this).val()},
          headers: {
            'X-CSRF-Token': '<%= form_authenticity_token.to_s %>'
          },
          url: "/get_files"
      });
    });
});  

the get_files action:

def get_files
    folder=params[:folder]+"/"
    @files= #getting all the files in a folder as array
    respond_to do |format|
       format.js
    end
end  

In get_files.js.erb:

$("#audio").html("<%=escape_javascript(render(@files)) %>");  

In new.html.erb:

<div id="audio">
  <%= render @files if @files %>
</div>  

In _files.html.erb:

<%= f.input :select_audio, collection: @files, as: :select %>  

But I'm getting:

> ArgumentError at /get_files

'"content.docx"' is not an ActiveModel-compatible object that returns a valid partial path.

here content.docx is the first file of the @files array. What is wrong here? I'm trying to get all the files of a folder from AWS s3. the user selects a folder and then ajax call is made to get_files which responds back with the files in a folder which is an array.
Even if I manually set the @files instance variable to say [1,2,3,4] I get the same error.

If I set @files="hello", it asks for a hello partial. why is this not working?

Upvotes: 1

Views: 94

Answers (2)

Rahul Singh
Rahul Singh

Reputation: 3427

first of all change

$("#audio").html("<%=escape_javascript(render(@files)) %>");

to

$("#audio").html("<%=escape_javascript(render partial: 'files', locals: { files:@files } ) %>"); 

and then in your partial u should write

<%= select_tag "select_audio", options_for_select(files) %>

for more info on select tag see select_tag

Upvotes: 1

Rahul garg
Rahul garg

Reputation: 9362

Looks like error in:

$("#audio").html("<%=escape_javascript(render(@files)) %>");  

I think it should be :

$("#audio").html("<%=escape_javascript(render 'files' ) %>"); 

to actually be able to render _files.html.erb partial

Upvotes: 1

Related Questions