0

I have an app in which users have submissions, and folders to store and organize their submissions into. In my view, I display a list of all of the users folders, and ideally when the user clicks on a folder, all of the submissions associated to the folder load into view.

The Problem

My view looks like this:

<div id="submission-list-container">
                <% current_user.folders.each do |folder| %>
                    <%= link_to folder_path(folder.id), :remote=> true, :id=>folder.id %>
                    <div class="post-container" id="<%= folder.id %>">
                        <%= folder.title %> <p id="created-time">Created <%= folder.created_at.strftime("%e/%-m") %></p>
                    </div>
                <% end %>
            </div>

Rails loops over each folder associated to the current_user and puts it into the view. As you can see, I have a link_to which goes to the path for the show action on the folders model. I pass in the id of each folder.

What's supposed to happen is that it routes to the show action in the folders controller, which looks like this:

def show
    @folder = Folder.find(params[:id])
    respond_to do |f|
        f.js 
        f.json
    end
end

The hyperlink looks like "/folders/1", which would correctly route to the show action. However, when I click the link I see the following in my console:

Processing by HomeController#index as JS
Parameters: {"id"=>"1"}
User Load (2.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
Folder Load (1.7ms)  SELECT "folders".* FROM "folders" WHERE "folders"."user_id" = 2

This is clearly wrong. It should loading the folder with the folder.id I passed through, not the current_user id.

I also have a file, title show.js.erb, which is located in /views/home. This file is meant to inject a partial in the .post-container class (just where I'm injecting it for this test). The file looks like this:

$(".post-container").append('<%=j render :partial => 'contents', :locals => {:folder => folder } %>');

I won't post my partial as this post is getting quite long, but I've tested it and know it works. Finally, for sake of clarity, I did a rake routes and got the following output (everything relevant to folders):

                 folders GET    /folders(.:format)                      folders#index
                     POST   /folders(.:format)                      folders#create
          new_folder GET    /folders/new(.:format)                  folders#new
         edit_folder GET    /folders/:id/edit(.:format)             folders#edit
              folder GET    /folders/:id(.:format)                  folders#show
                     PUT    /folders/:id(.:format)                  folders#update
                     DELETE /folders/:id(.:format)                  folders#destroy

As a Rails newbie, I have attempted to debug this problem over and over to no avail. Google searches also haven't been of much help, which is why I come to you guys and gals. Even if you aren't sure about the problem, advice on where to start looking would be much appreciated.

Thanks!

Tom Maxwell
  • 9,273
  • 17
  • 55
  • 68

1 Answers1

0

Seems your problem is you should be using:

<%= link_to 'Folder ' + folder.id, folder_path(folder), :remote=> true %>

instead of:

<%= link_to folder_path(folder.id), :remote=> true, :id=>folder.id %>

As this does not match any signature from docs.

sites
  • 21,417
  • 17
  • 87
  • 146