Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

Tressa S.1's avatar
Tressa S.1
New member | Level 1
10 years ago
Solved

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

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?

  • 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.

1 Reply

Replies have been turned off for this discussion
  • Tressa S.1's avatar
    Tressa S.1
    New member | Level 1
    10 years ago

    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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!