导轨的Ajax渲染部分(投票系统)导轨、投票系统、部分、Ajax

2023-09-10 18:34:42 作者:空谷幽兰

我无法再渲染的部分通过Ajax请求。我可以通过js文件目前正在通过其他的东西,如警报(),甚至改变当前DIV的HTML。但是,当我试图使部分(等级,在这种情况下),什么都不会刷新。谐音做工精细,因为它们呈现在分页和负载感,但不是在我单击upvote或downvote。我使用嵌套的谐音,使其首先渲染_tracks,然后_rating。这是我have..what我缺少什么?

vote.js.erb

  $(等级)HTML(<%= escape_javascript(渲染(:部分=>'评级'))%>中)
 

有了这个,我也是这些,没有成功。

  $(等级)HTML(<%= escape_javascript(渲染(等级))%>中)
   $(等级)HTML(<%= escape_javascript(渲染(:部分=>中等级,:当地人=> {:轨道=>轨道}))%>中)
 

这,在另一方面,没有工作:

  $(等级)HTML(<%= escape_javascript(测试文本在这里)%>中)
 
KKBOX 给你在线听歌全新感觉

_tracks.html.erb

 <%的轨道@tracks%GT;
            < D​​IV CLASS =span4>
                < D​​IV CLASS =跟踪>
                    < D​​IV CLASS =评级>
                        <%=渲染:部分=> 评级,:当地人=> {:轨道=>轨道}%GT;
                    < / DIV>

                    &所述; A HREF =&其中;%= track.youtube_link%>中>&所述; / a取代;

                    <如果current_user.admin%? %>
                        <%=的link_to删除,轨道,方法:你确定:删除,确认%>
                        <%=的link_to编辑,edit_track_path(轨道)%>
                    <%结束%GT;
                < / DIV>
            < / DIV>
<%结束%GT;
 

_rating.html.erb

 <%VT = track.votetype(CURRENT_USER)%>

                <%如果VT =向上%GT!;
                    <%=的link_to image_tag(up_arrow.png,ALT:Upvote),vote_path(轨道,:类型=>中达),:远程=>真%>
                <%ELSIF%GT;
                    <%=向上%GT;
                <%结束%GT;

                &其中;%= track.rating%GT;

                <%!如果VT =下%>
                    <%=的link_to image_tag(down_arrow.png,ALT:Downvote),vote_path(轨道,:类型=>中下),:远程=>真%>
                <%ELSIF%GT;
                    <%=下降%>
                <%结束%GT;
 

跟踪控制器

  @ vote.update_attributes(:vote_type => PARAMS [:类型])
        如果@ vote.save
      respond_to代码做|格式|
        的format.html {redirect_to时:回}
        format.js
      结束
 

解决方案

  $(等级)HTML(<%= escape_javascript(渲染(:部分=> 评级,:当地人=> {:轨道=>轨道}))%>中)
 

必须是

  $(等级)HTML(<%= escape_javascript(渲染(:部分=>中等级,:当地人=> {:轨道=&GT ; @track}))%>中)
 

@符号通过变量,不同的是循环在previous部分,你并不需要它。现在工作的罚款。

I am having trouble re-rendering a partial through an Ajax request. I can currently pass other things through the js file such as alert() and even changing the html in the current div. But when I try and render the partial (rating, in this instance), nothing will refresh. The partials work fine in the sense that they render on pagination and on load, but not after I click an upvote or downvote. I'm using nested partials so that it renders _tracks first, then _rating. Here's what I have..what am I missing?

vote.js.erb

$(".rating").html("<%= escape_javascript(render(:partial => 'rating')) %>")

With this I also these, with no success.

   $(".rating").html("<%= escape_javascript(render("rating")) %>")
   $(".rating").html("<%= escape_javascript( render( :partial => "rating", :locals => { :track => track }) ) %>")

This, on the other hand, does work:

$(".rating").html("<%= escape_javascript("test text here) %>")

_tracks.html.erb

    <% for track in @tracks %>
            <div class="span4">
                <div class="track">
                    <div class="rating">
                        <%= render :partial => "rating", :locals => { :track => track } %>
                    </div>

                    <a href="<%= track.youtube_link %>"></a>

                    <% if current_user.admin? %> 
                        <%= link_to "delete", track, method: :delete, confirm: "You sure?" %>
                        <%= link_to "edit", edit_track_path(track) %>
                    <% end %>
                </div>
            </div>
<% end %>

_rating.html.erb

<% vt = track.votetype(current_user) %>

                <% if vt != "up" %>
                    <%= link_to image_tag("up_arrow.png", alt: "Upvote"), vote_path(track, :type => "up"), :remote => true %>
                <% elsif %>
                    <%= "up" %>
                <% end %>

                <%= track.rating %>

                <% if vt != "down" %>
                    <%= link_to image_tag("down_arrow.png", alt: "Downvote"), vote_path(track, :type => "down"), :remote => true %>
                <% elsif %>
                    <%= "down" %>
                <% end %>

track controller

@vote.update_attributes(:vote_type => params[:type])
        if @vote.save
      respond_to do |format|
        format.html {redirect_to :back}
        format.js
      end

解决方案

$(".rating").html("<%= escape_javascript( render( :partial => "rating", :locals => { :track => track }) ) %>")

needs to be

$(".rating").html("<%= escape_javascript( render( :partial => "rating", :locals => { :track => @track }) ) %>")

The @ symbol passes the variable through, unlike the for loop in the previous partial where you didn't need it. Working fine now.