将 div 标签添加到我的 .js.erb 文件时,Ajax 功能似乎失败

发布于 2024-11-07 20:39:46 字数 3643 浏览 2 评论 0原文

我正在我的代码中做一些样式设置,我想在我的应用程序的群组论坛上显示消息发布的时间。目前,该论坛中发布的每条消息都使用通过 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 文件中的上述内容相对应的代码部分( 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
 

这是一个部分视图,其中我拥有最终在 .js.erb 文件中使用的所有 我可以在不刷新页面的情况下解决这个问题吗?我做错了什么?

编辑

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;
    })
})

I am doing some styling in my code where I want to display the time a message is posted on a groups forum in my app. Currently posting of every message in this forum works with AJAX functionality implemented via jQuery 1.4.

On including the div tag in my post_message.js.erb file, I find that the message doesn't post on the groups home page (the place where I have all the group specific discussions - a functionality similar to what one would get to see in FB groups).

The message gets saved in my DB and on refreshing the page I get to see the latest message posted.

The code in post_message.js.erb looks like this:

$("#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();

The above code stops functioning properly when I change the second line to:-

$("#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/>");

The part of the code corresponding the above in _common.html.erb file (this is a partial view where I have all the html tags and id's which I am eventually using in my .js.erb file) looks like this:

<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>

My post_message method in my groups controller looks like this:

 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
 

How can I fix this without refreshing the page? What am I doing wrong?

Edit

group.js file code:

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;
    })
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒夜笙歌凉 2024-11-14 20:39:46

我可以通过向围绕我想要隐藏或显示的控件的页面添加另一个标签来解决此问题。

我无法让 div 的 display:none 以编程方式工作,也无法让 asp:Panel 来切换其可见性。

在 AJAX 能够更改显示参数之前,ASP.NET 正在渲染页面,特别是我想要隐藏或显示的部分。 UpdateMode="Always" 也向 AJAX 发出更新其他部分的信号。

I was able to fix this problem by adding another tag to the page surrounding the controls I wanted to conceal or reveal.

I could not get either a div's display:none to work programatically, or an asp:Panel to toggle its visibility.

ASP.NET was rendering the page, in paticular the parts of it I wanted to conceal or reveal, before AJAX was able to change the display parameters. UpdateMode="Always" signaled to AJAX update other parts, too.

够运 2024-11-14 20:39:46

你能确认你的ajax正在工作吗?我本来希望看到 ':remote =>; true' 在你的表单标签中。如果它不存在,我认为您的 format.js 响应不会被调用。

form_for  :group_post, @group_post, :remote=>true ...

我认为当你刷新页面时,它不会调用 js 代码,而只是调用 format.html 。要进行检查,您可以检查日志或在格式块中放置打印语句,然后查看它是否被打印。

format.js do
    puts "I am here"
end

Can you confirm your ajax is working? I would have expected to see a ':remote => true' in your form tag. If its not there I wouldn't think your format.js response would get called.

form_for  :group_post, @group_post, :remote=>true ...

I think that when you do a page refresh it isn't calling the js code it's just calling the format.html instead. To check you could check your log or put a print statement in the format block and see if it gets printed.

format.js do
    puts "I am here"
end
佞臣 2024-11-14 20:39:46

通过删除在定义样式表类时使用的额外引号(“)来修复该错误,在 post_message.js.erb 中已经有一对带 prepend 方法的引号。

The bug was fixed by removing the extra quotes(") made use while defining the class wrt the stylesheet, there were already a pair of quotes with the prepend method.. in post_message.js.erb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文