cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you an educator? Well we want to hear from you, so check out some tools we think might help you out here!

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to display the contents of folder on index page when link is clicked in rails?

How to display the contents of folder on index page when link is clicked in rails?

Tressa S.1
New member | Level 1

I have code in rails 4.2.3 where on the index page of the dropbox connect feature, it creates a list of the files in the user's root dropbox directory once logged in. This is the code:

def index
    @druidbox = current_user.dropbox_session if current_user.dropbox_session
# find the current user's dropbox_session from the database
    if current_user.dropbox_session
      dbsession = DropboxSession.deserialize(current_user.dropbox_session)
# create the dropbox client object
      @client = DropboxClient.new(dbsession, DROPBOX_APP_MODE).metadata('/')
   end
end

 

In this list created, you can see and download any files at the root level. You can see any directory at the root level. The directory is even linked but when clicked, it refreshes the page but doesn't change the contents of the view.  This is the code I have in the index view:

 

<tbody>
<% @client["contents"].each do |key, value| %>
<tr>
<td>
<% if (key["is_dir"]) %>
<%= "<div class='glyphicon glyphicon-folder-open'></div> ".html_safe %> &nbsp;
<%= link_to key["path"][1..-1], dropbox_path_change_path(path: key["path"]) %> &nbsp;
<%else %>
<%= key["path"][1..-1]%> &nbsp;
<%= link_to "[#{key["size"]} <div class='glyphicon glyphicon-cloud-download'></div>]".html_safe, dropbox_download_path(path: key["path"]), :target => "_blank" %>
<%end%>
</td>
</tr>
<%end%>
<%end%>
</tbody>

 

dropbox_path_change_path is just a partial that reloads the index. I was thinking that when someone clicks on the link for a folder, it would reload the index but list the contents of the directory the link goes to.

 

How do I display the contents of the folder on the index page when the link is clicked?

1 Accepted Solution

Accepted Solutions

Re: How to display the contents of folder on index page when link is clicked in rails?

Tressa S.1
New member | Level 1

For anyone else that needs help, I did get this to work for my situation using rails.

First off, I followed this tutorial which he admitted wasn't tested so I did have to make changes to get it to work and this was one of them: http://blog.jobspire.net/dropbox-on-rails-4-0-ajax-using-dropbox-sdk/

To get the directory views to work, I created this bit of code in the controller:

def folder
@dropbox = current_user.dropbox_session if current_user.dropbox_session
if current_user.dropbox_session
dbsession = DropboxSession.deserialize(current_user.dropbox_session)
@client = DropboxClient.new(dbsession, DROPBOX_APP_MODE).metadata("#{params[:path]}/#{params[:file]}")
end
end

Then I created a view called folder.html.erb. It's an exact copy of the main index.html.erb view.  

Then I created a partial called dropbox_path_change where I did have to change the code I listed above to use folder_path instead of dropbox_path_change_path.

Then I added the folder_path route to my routes file and now users can navigate into the folders at any level.

View solution in original post

1 Reply 1

Re: How to display the contents of folder on index page when link is clicked in rails?

Tressa S.1
New member | Level 1

For anyone else that needs help, I did get this to work for my situation using rails.

First off, I followed this tutorial which he admitted wasn't tested so I did have to make changes to get it to work and this was one of them: http://blog.jobspire.net/dropbox-on-rails-4-0-ajax-using-dropbox-sdk/

To get the directory views to work, I created this bit of code in the controller:

def folder
@dropbox = current_user.dropbox_session if current_user.dropbox_session
if current_user.dropbox_session
dbsession = DropboxSession.deserialize(current_user.dropbox_session)
@client = DropboxClient.new(dbsession, DROPBOX_APP_MODE).metadata("#{params[:path]}/#{params[:file]}")
end
end

Then I created a view called folder.html.erb. It's an exact copy of the main index.html.erb view.  

Then I created a partial called dropbox_path_change where I did have to change the code I listed above to use folder_path instead of dropbox_path_change_path.

Then I added the folder_path route to my routes file and now users can navigate into the folders at any level.

View solution in original post

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    Tressa S.1 New member | Level 1
What do Dropbox user levels mean?
Need more support?