我正在我的代码中进行一些样式设置,我想在我的应用程序中的群组论坛上显示消息发布的时间。目前,该论坛中发布的每条消息都使用通过 jQuery 1.4 实现的 AJAX 功能。
在我的
post_message.js.erb
文件中包含 div 标签时,我发现该消息不会发布在组主页上(我进行所有组特定讨论的地方 - 类似于人们在FB群组)。
消息被保存在我的数据库中,刷新页面后我可以看到发布的最新消息。
post_message.js.erb 中的代码如下所示:
$("#new_post").before('<div id ="new_post"><%= escape_javascript(flash.delete(:notice)) %></div>');<!--TO-DO the flash messages aren't yet enabled/working-->
$("#latest_post").prepend("<%= @group_post.message %> by <%= Investor.find(@group_post.post_by).first_name %> <%= distance_of_time_in_words(@group_post.created_at,Time.now) %> ago <br/><br/><hr/>");
$("#new_post")[0].reset();
当我将第二行更改为:-
时,上述代码将停止正常运行$("#latest_post").prepend("<%= @group_post.message %> by <%= Investor.find(@group_post.post_by).first_name %> <div class ="contentdispgrp"> <%= distance_of_time_in_words(@group_post.created_at,Time.now) %> ago </div><br/><br/><hr/>");
_common.html.erb
文件中与上述内容相对应的代码部分(这是一个部分视图,其中我拥有最终在我的.js.erb
文件中使用的所有html标签和id)如下所示:
<table>
<tr>
<%if @current_user.is_an_existing_member_of_group(@investor_group)%>
<%form_for :group_post, @group_post, :url => {:action => :post_message, :id => params[:id]},:html => {:multipart => 'true', :id => 'new_post'} do |f| -%>
Start Discussion:<br><%=f.text_field :message%>
<!--<%=f.file_field :photo%> -->
<%=submit_tag "Post"%></p>
<%end%>
<div id = "latest_post"> </div>
<%for a in @group_all_posts %>
<%= a.message %> by <%= Investor.find(a.post_by).first_name %> <div class ="contentdispgrp" id="style_chck"> <%= distance_of_time_in_words(a.created_at,Time.now) %> ago </div><br/><br/> <hr/>
<%end%>
</tr>
<%end%>
</table>
我的组控制器中的 post_message 方法如下所示:
def post_message
@investor_group = InvestorGroup.find(params[:id])
unless @current_user.is_an_existing_member_of_group(@investor_group)
flash[:notice] = "Please join this group to participate in discussions"
redirect_to :action => :show, :id => @investor_group and return # try to find out what exactly does this command do with return stmnt
else
@group_post = GroupPost.new(params[:group_post])
end
#@group_post = GroupPost.new(params[:group_post])
investor_id = session['investor_id']
@group_post.investor_group_id = @investor_group.id
@group_post.post_by = investor_id
if @group_post.save
flash[:notice] = 'Post was successfully created.'
# redirect_to :action => :show, :id => params[:id] - removed after trying to implement ajax via jquery
else
flash[:notice] = 'Post was not successfully created.'
end
respond_to do |format|
format.html {redirect_to :action => "show", :id => params[:id]}
format.js
end
end
如何在不刷新页面的情况下解决此问题?我做错了什么?
group.js文件代码:
jQuery.ajaxSetup({
'beforeSend' : function(xhr) {
xhr.setRequestHeader("Accept","text/javascript")
}
})
$(document).ready(function(){
$("#new_post").submit(function(){
$.post($(this).attr("action"),$(this).serialize(),null,"script");
return false;
})
})
我可以通过在页面上围绕我想要隐藏或显示的控件添加另一个标签来解决这个问题。
我无法让 div 的 display:none 以编程方式工作,也无法让 asp:Panel 来切换其可见性。
在 AJAX 能够更改显示参数之前,ASP.NET 正在渲染页面,特别是我想要隐藏或显示的部分。 UpdateMode="Always" 也向 AJAX 发出更新其他部分的信号。
你能确认你的ajax正在工作吗?我希望在您的表单标签中看到“:remote => true”。如果它不存在,我认为您的 format.js 响应不会被调用。
form_for :group_post, @group_post, :remote=>true ...
我认为当您刷新页面时,它不会调用 js 代码,而只是调用 format.html 。要进行检查,您可以检查日志或在格式块中放置打印语句,然后查看它是否被打印。
format.js do
puts "I am here"
end
通过删除在定义样式表类时使用的额外引号(“)来修复该错误,在
post_message.js.erb.
中已经有一对带有前置方法的引号。