重新加载Rails部分ajax回调
Reload Rails partial on ajax callback
在我的Rails应用程序中,用户有可以在其中创建新博客文章的文件夹。我想在用户创建新帖子时刷新包含所有文件夹帖子的部分。我试图在文件夹控制器中创建一个操作,只需再次调用分部并替换它,但我收到了CanCan::AccessDenied 500错误。
在我的index.html.erb中,这是我的文件夹显示部分的链接:
<%= link_to folder_path(folder), remote: true %>
以下是加载到视图中的部分:
<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>
正如我所说,我想重新加载整个部分,这样就可以反映新数据,而无需用户执行完整的页面刷新。
我在文件夹控制器中创建了一个新的操作,看起来像这样:
def ajax_load_events
respond_to do |format|
format.js
format.html
end
end
Ajax_load_events.js:
$("#panel-container").replace_html('<%= escape_javascript(render(:partial => "contents", :locals => {:folder => @folder})).html_safe %>');
最后,我的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");
}
});
})
这是我的路线。rb:
get "/ajax_load_events(.:format)" => "folders#ajax_load_events"
您可能没有正确定义用户能力。尝试在能力文件的底部添加以下行之一:
can :ajax_load_events, Folder
这允许用户执行特定的控制器动作。
或者:
can :manage, Folder
这允许用户在资源上执行所有类型的动作(不仅仅是CRUD)。
相关文章:
- Rails部分未呈现
- Rails 4、Turbolink在渲染一个部分后执行javascript
- 如何使用 javascript 而不是使用控制器中的方法在 rails 中呈现部分按钮单击
- Rails Ajax 刷新部分,同时持久化参数
- Rails:尝试在按钮单击时渲染部分
- Rails使用ajax调用更新多态对象的表部分
- 将Ajax部分视图模板放在Assets/javascripts目录中,而不是视图目录、Rails中
- 为什么不是't在我的rails部分中工作的DataTables
- 基于媒体查询的Rails部分加载
- 在jQuery单击函数上渲染部分Rails
- Rails - 如何通过 AJAX 在渲染的部分中调用 jQuery 事件
- 在 Rails 中使用 AJAX link_to渲染部分
- Ruby on Rails - 执行 ajax 以在 JQuery 单击时呈现部分
- 重新加载Rails部分ajax回调
- Rails部分未加载到视图中
- Rails部分带有Javascript的监听器可以在主页上工作,但在其他地方不起作用
- Ajax加载静态Rails部分
- Rails部分不生成事件
- 在选择更改时渲染rails部分
- 通过jquery使用集合更新rails部分