简体   繁体   English

Rails部分无法加载到视图中?

[英]Rails partial not loading into view?

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. Rails遍历与current_user关联的每个文件夹,并将其放入视图中。 As you can see, I have a link_to which goes to the path for the show action on the folders model. 如您所见,我有一个link_to ,它转到文件夹模型上show动作的路径。 I pass in the id of each folder. 我传入每个文件夹的ID。

What's supposed to happen is that it routes to the show action in the folders controller, which looks like this: 应该发生的是,它路由到文件夹控制器中的show操作,如下所示:

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. 超链接看起来像“ / folders / 1”,它将正确地路由到show操作。 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. 它应该使用我通过的folder.id而不是current_user id加载文件夹。

I also have a file, title show.js.erb, which is located in /views/home. 我还有一个名为show.js.erb的文件,该文件位于/ views / home中。 This file is meant to inject a partial in the .post-container class (just where I'm injecting it for this test). 该文件旨在在.post-container类中注入部分.post-container (恰好是我为此测试注入的位置)。 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): 最后,为清楚起见,我进行了一次rake routes并获得以下输出(与文件夹相关的所有信息):

                 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. 作为Rails的新手,我试图一遍又一遍地调试此问题,但无济于事。 Google searches also haven't been of much help, which is why I come to you guys and gals. Google搜索也没有多大帮助,这就是为什么我来找你们和加尔斯。 Even if you aren't sure about the problem, advice on where to start looking would be much appreciated. 即使您不确定该问题,也将非常感谢您从哪里开始寻找建议。

Thanks! 谢谢!

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 . 因为这与docs的任何签名都不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM