简体   繁体   English

在Ajax回调中重新加载Rails部分

[英]Reload Rails partial on ajax callback

In my Rails app users have folders in which they can create new blog posts. 在我的Rails应用程序中,用户具有文件夹,可以在其中创建新的博客文章。 I'd like to refresh the partial containing all the folder posts when the user creates a new one. 当用户创建新文件夹时,我想刷新包含所有文件夹帖子的部分。 I've tried to create an action in the folders controller that just call the partial again and replaces it, but I'm getting a CanCan::AccessDenied 500 error. 我试图在文件夹控制器中创建一个动作,该动作只是再次调用了partial并将其替换,但是我遇到了CanCan :: AccessDenied 500错误。

In my index.html.erb, this is the link to my folders show partial: 在我的index.html.erb中,这是指向我的文件夹的链接,其中显示了部分内容:

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

Here's the partial that is loaded into view: 这是加载到视图中的部分内容:

    <div id="panel-container">
    <div id="entry-container" class="entry-window">
        <div id="meta-bar" class="top">
            <span id="location-span" style="position:relative; float:left;">In <%= @folder.title %></span>
            <div id="entry-meta" class="meta-info">
                <i class="icon-ok icons" id="save-entry"></i>
                <i class="icon-remove icons" id="close" style="margin-left:10px"></i>
            </div>
        </div>
        <div id="entry-create-partial" class="contain-entry">
            <div id="title-create-partial" class="title-partial" name="title" contenteditable="true" data-placeholder='Title it' style="color:black"></div>
            <div id="meta-popup"><button type="button" class="btn" id="nested-button">Create Nested Entry</button></div>
            <div id="content-create-partial" class="content-partial" name="content" contenteditable="true" data-placeholder='Write anything' style="color:gray"></div>
        </div>
    </div>

    <div id="container-edit" class="entry-window">
        <span id="location-span" style="position:relative; float:left;">In <%= @folder.title %></span>
        <div id="entry-meta" class="meta-info">
            <button type="button" id="save-edit" class="btn">Save</button>
            <button type="button" class="btn" id="close-edit"><i class="icon-remove" id="entry-cancel-x close-edit"></i>Cancel</button>
        </div>
        <div id="entry-edit-partial" class="contain-entry">
            <div id="title-edit-partial" class="title-partial" name="title" contenteditable="true" style="color:black"></div>
            <div id="content-edit-partial" class="content-partial" name="content" contenteditable="true" style="color:gray"></div>
        </div>
    </div>

    <div id="right-panel">
        <div id="top" class="top">
            <h3><%= @folder.title %></h3>
            <ul id="inner-list">
                <button class="btn" id="journal-create-button">New Journal</button>
                <div id="create-journal-drop">
                    <form action="/folders" method="post">
                        <%= hidden_field_tag :user_id, current_user.id %> 
                        <%= hidden_field_tag :parent_id, @folder.id %>
                        <h3 id="journal-create-partial">New Sub-Journal</h3>
                        <input type="text" id="titleinput" name="title" placeholder="Title it"></input>
                        <button type="submit" class="md-close folder-create" id="folder-create">Create</button>
                    </form>
                </div>
                <button class="btn" id="entry-button">New Entry</button>
                <button type="button" id="delete-button" class="btn"><i class="icon-remove"></i></button>
            </ul>
        </div>
        <% if @folder.submissions.count > 0 %>
        <svg id="submission-circles">
            <circle></circle>
            <circle></circle>
            <circle></circle>
            <circle></circle>
        </svg>
        <% else %>
        <h2>It's quiet in here. Create a journal entry or sub-journal to get started!</h2>
        <% end %>
    </div>
</div>

As I said, I'd like to reload this whole partial so that the new data is reflected without the user having to perform a full page refresh. 正如我所说,我想重新加载整个部分内容,以便反映新数据,而无需用户执行整页刷新。

I created a new action in the folders controller that looks like this: 我在文件夹控制器中创建了一个新操作,如下所示:

def ajax_load_events
    respond_to do |format|
        format.js
        format.html
    end
end

Ajax_load_events.js: Ajax_load_events.js:

$("#panel-container").replace_html('<%= escape_javascript(render(:partial => "contents", :locals => {:folder => @folder})).html_safe %>');

And finally, my AJAX call: 最后,我的AJAX呼叫:

$("#save-entry").click(function(){
            $.ajax({
                type: "POST",
                url: "/submissions",
                dataType: "json",
                data: {title: $("#title-create-partial").text(), content: $("#content-create-partial").text(), folder_id: <%= @folder.id %>},
                complete: function(){
                    $.get("/ajax_load_events/", {folder: <%= @folder.id %>}, null, "script");
                }
            });
        })

Here's the route in my routes.rb: 这是我的routes.rb中的路线:

get "/ajax_load_events(.:format)" => "folders#ajax_load_events"

You may not be defining the user abilities correctly. 您可能没有正确定义用户能力。 Try adding one of the following lines to the bottom of the abilities file: 尝试将以下几行之一添加到功能文件的底部:

can :ajax_load_events, Folder

which allows the user to perform that specific controller action. 允许用户执行该特定的控制器操作。

Or: 要么:

can :manage, Folder

which allows the user to perform all kind of actions over the resource (not only CRUD). 这允许用户对资源执行所有类型的操作(不仅限于CRUD)。

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

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