Ruby on rails 如何部分重新加载页面?RubyonRails

Ruby on rails 如何部分重新加载页面?RubyonRails,ruby-on-rails,ajax,render,Ruby On Rails,Ajax,Render,我正在创建一个播客页面。除了标签过滤,它几乎完成了 在索引文件中,所有播客列表位于底部,列出了所有标记并可单击过滤器 = raw Podcast.tag_counts.map{ |p| link_to p.name, podcasts_path(tag: p.name)}.join(' ') 我正在挣扎的是,当我点击一个标签时,它会重新加载整个页面,并在顶部停止,而我的所有播客列表位于底部 因此,我考虑部分重新加载而不是整个页面重新加载,并找到了一篇使用Ajax调用的文章。但是,这并

我正在创建一个播客页面。除了标签过滤,它几乎完成了

在索引文件中,所有播客列表位于底部,列出了所有标记并可单击过滤器

= raw Podcast.tag_counts.map{ |p| link_to p.name, podcasts_path(tag: p.name)}.join(' ')     
我正在挣扎的是,当我点击一个标签时,它会重新加载整个页面,并在顶部停止,而我的所有播客列表位于底部

因此,我考虑部分重新加载而不是整个页面重新加载,并找到了一篇使用Ajax调用的文章。但是,这并没有改变任何事情。它正在重新加载整个页面

index.html.haml

 %section#podcast_list
      =render 'podcasts_list'
= raw Podcast.tag_counts.map{ |p| link_to p.name, podcasts_path(tag: p.name)}.join(' ')
  
  - @podcasts.each do |podcast|  
    .columns.large-6.m-top4
      .columns.large-12
        %h4
         =podcast.id
         = truncate(podcast.episode_title, :length=>60)
        %p{:style=>"text-transform:capitalize;"}
          %b
            = podcast.tag_list.join(' | ')
            
        = podcast.impressionist_count(:filter=>:ip_address) 
        Views
        
.row
  .columns.large-12.m-top3
    = will_paginate @podcasts, previous_label: "Previous", next_label: "Next", :class=>"digg_pagination"
_podcast_list.html.haml

 %section#podcast_list
      =render 'podcasts_list'
= raw Podcast.tag_counts.map{ |p| link_to p.name, podcasts_path(tag: p.name)}.join(' ')
  
  - @podcasts.each do |podcast|  
    .columns.large-6.m-top4
      .columns.large-12
        %h4
         =podcast.id
         = truncate(podcast.episode_title, :length=>60)
        %p{:style=>"text-transform:capitalize;"}
          %b
            = podcast.tag_list.join(' | ')
            
        = podcast.impressionist_count(:filter=>:ip_address) 
        Views
        
.row
  .columns.large-12.m-top3
    = will_paginate @podcasts, previous_label: "Previous", next_label: "Next", :class=>"digg_pagination"
index.js.erb

$("#podcast_list").html("<%= escape_javascript(render("podcast_list")) %>");
$(“#播客列表”).html(“”);
podcast.controller.rb

class PodcastsController < ApplicationController
  before_action :find_podcast, only: [:show, :edit, :update, :destroy]
  before_filter :authorize, only: [:edit, :update, :new, :create, :destroy]
  impressionist :actions=>[:show,:index]
  
  def index
   if params[:tag].present?
      @podcasts = Podcast.all.tagged_with(params[:tag]).paginate(page: params[:page], per_page: 4)
    else
      @podcasts =Podcast.all.paginate(page: params[:page], per_page: 4)
    end  
    
  
  end
class-PodcastsController[:显示,:索引]
def索引
如果参数[:标记]。是否存在?
@Podcast=Podcast.all.taged_with(params[:tag]).paginate(page:params[:page],per_page:4)
其他的
@Podcast=Podcast.all.paginate(第页:参数[:页],每页:4)
结束
结束
我是错过了什么还是做错了?我在Ajax方面没有任何经验

如果这不是一个好方法,有没有其他方法可以实现我的目标

我的目标是显示包含所选标签的播客列表,而无需用户执行额外操作(当前需要在重新加载页面后向下滚动到底部)

感谢您抽出时间阅读本文并提前提供帮助

在HTML链接中应用:remote=>task之后。下面有个错误

Completed 500 Internal Server Error in 54ms
Completed 500 Internal Server Error in 54ms

ActionView::Template::Error (Missing partial podcasts/_podcast_list, application/_podcast_list with {:locale=>[:en], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
  * "/home/ubuntu/environment/joynus-test/app/views"
  * "/home/ubuntu/.rvm/gems/ruby-2.3.6@joynus/gems/shareable-1.1.4/app/views"
  * "/home/ubuntu/.rvm/gems/ruby-2.3.6@joynus/gems/kaminari-0.16.1/app/views"
  * "/home/ubuntu/.rvm/gems/ruby-2.3.6@joynus/gems/foundation-rails-5.4.5.0/app/views"
  * "/home/ubuntu/.rvm/gems/ruby-2.3.6@joynus/bundler/gems/ckeditor-fdedb6245542/app/views"
):
    1: $("#podcast_list").html("<%= escape_javascript(render("podcast_list")) %>");
  app/views/podcasts/index.js.erb:1:in `_app_views_podcasts_index_js_erb__1632310405876044147_47038950809000'
在54毫秒内完成500个内部服务器错误
在54毫秒内完成500个内部服务器错误
ActionView::Template::Error(缺少部分播客/播客列表,应用程序/播客列表,带有{:locale=>[:en],:formats=>[:js,“application/ecmascript”,“application/x-ecmascript”,:html,:text,:css,:ics,:csv,:vcf,:png,:jpeg,:gif,:bmp,:tiff,:mpeg,:xml,:rss,:atom,:yaml,:多部分格式,:url编码格式,:json,:pdf,:zip],:variants=>[],:handlers=>[:erb,:builder,:raw,:ruby,:coffee,:haml,:jbuilder]}。在以下位置搜索:
*“/home/ubuntu/environment/joynus test/app/views”
*“/home/ubuntu/.rvm/gems/ruby-2.3。6@joynus/gems/shareable-1.1.4/应用程序/视图”
*“/home/ubuntu/.rvm/gems/ruby-2.3。6@joynus/gems/kaminari-0.16.1/app/views”
*“/home/ubuntu/.rvm/gems/ruby-2.3。6@joynus/gems/foundation-rails-5.4.5.0/app/views“
*“/home/ubuntu/.rvm/gems/ruby-2.3。6@joynus/捆绑机/gems/ckeditor-fdedb6245542/app/views”
):
1:$(“#播客列表”).html(“”);
app/views/podcasts/index.js.erb:1:in`_app_views_podcasts_index_js_erb_1632110405876044147_47038950809000'

您需要用
remote:true
标记链接,该链接将停止默认操作,并将其替换为对相应控制器操作的异步调用。因此,在你的情况下:

link_to p.name, podcasts_path(tag: p.name), remote: true
编辑


关于您现在遇到的
TemplateError
:我相信您遇到的问题是Javascript位于.js.erb文件中,因此呈现调用正在查找不存在的
podcast/\u podcast\u list.html.erb
。您可以通过本文了解如何将Javascript建模为HAML文件,以便它使用HAML进行渲染。我不使用HAML,所以可能有一种简单的方法来混合ERB和HAML,就像我不知道的那样。

=raw Podcast.tag|u counts.map{p|link|u to p.name,podcasts|u path(tag:p.name),remote:true}.join(“”)
我修改了链接,不再重新加载页面。但是,现在过滤器不工作。我的控制台出现错误(附屏幕截图)。我遗漏了什么吗?你能在你的问题中添加你的控制器代码吗?500错误表明服务器端存在问题,因此检查Rails日志也是一个很好的开始。我怀疑javascript呈现调用是错误的。。不确定,这是个愚蠢的错误。我创建了部分名称“podcast\u list”,并尝试在javascript中调用“podcast\u list”。所以基本上,你添加“remote:true”的评论已经解决了我的问题。谢谢