渲染后,浏览器不会刷新页面

问题描述 投票:0回答:2

我目前仍受博客文章搜索功能的束缚。控制器和html渲染似乎运行良好,但是我看不到Chrome浏览器的任何更新。

从搜索场景开始,如下所示转到_navbar.html.erb

                        <%= form_with url: admin_posts_path, method: "get" do %>
                            <%= text_field_tag :search, params[:search], id: "input-search", placeholder: "Search Posts" %>
                        <% end %>

,并进入以下的posts_controller.rb

  def index
    if params[:search]
      @posts = Post.search(params[:search]).all.order('created_at DESC').paginate(per_page: 3, page: params[:page])
      puts "&&&&&&&&&&&&&&&&&"
      p @posts
      puts "###################"
    else
      @posts = Post.all.order('created_at DESC').paginate(per_page: 3, page: params[:page])
    end
  end

我发现@posts的值通过使用服务器日志中的puts得到了正确的结果。但是,最终在按如下所示渲染index.html.erb之后,浏览器根本不更新任何渲染的html。它只是停留在当前页面。

<div class="main">
    <section>
        <a href="<%= new_admin_post_path %>"><button class="create-button">Create New</button></a>
        <% if @posts.exists? %>
            <% @posts.each do |post| %>
                <article class="article-summary">
                    <h1 class="article-title"><%= post.title %></h1>
                    <p class="article-content"><%= truncate post.body, length: 200 %></p>
                    <p class="article-created-at"><%= post.created_at.to_time.strftime('%B %e at %l:%M %p') %></p>
                    <p class="article-command"><a href="<%= edit_admin_post_path(post) %>"><button>Edit</button></a> <%= link_to button_tag("Delete"), admin_post_path(post), method: :delete, data: { confirm: 'Are you sure?' } %></p>
                    <%= image_tag 'https://placekitten.com/1000/400', class: "article-image" %>
                </article>
            <% end %>
            <%= will_paginate @posts, class: "page"%>
        <% else %>
            <h2>There is no post</h2>
        <% end %>
    </section>
</div>

[当我看到服务器日志时,它肯定会显示搜索结果,但是我不知道为什么浏览器屏幕没有任何变化。

这是服务器日志

Started GET "/admin/posts?search=nine" for ::1 at 2020-04-29 00:03:15 +1000
Processing by Admin::PostsController#index as JS
  Parameters: {"search"=>"nine"}
&&&&&&&&&&&&&&&&&
  Post Load (1.1ms)  SELECT "posts".* FROM "posts" WHERE (title like '%nine%' OR body like '%nine%') ORDER BY created_at DESC LIMIT $1 OFFSET $2  [["LIMIT", 3], ["OFFSET", 0]]
  ↳ app/controllers/admin/posts_controller.rb:45:in `p'
#<ActiveRecord::Relation [#<Post id: 9, title: "Blog Post nine", category_id: 1, user_id: 3, tags: "rails", image: nil, body: "Lorem ipsum dolor sit amet, consectetur adipiscing...", created_at: "2020-04-28 12:41:46", updated_at: "2020-04-28 12:41:46">]>
###################
  Rendering admin/posts/index.html.erb within layouts/admin/application
  Post Exists? (0.4ms)  SELECT 1 AS one FROM "posts" WHERE (title like '%nine%' OR body like '%nine%') LIMIT $1 OFFSET $2  [["LIMIT", 1], ["OFFSET", 0]]
  ↳ app/views/admin/posts/index.html.erb:4
  CACHE Post Load (0.0ms)  SELECT "posts".* FROM "posts" WHERE (title like '%nine%' OR body like '%nine%') ORDER BY created_at DESC LIMIT $1 OFFSET $2  [["LIMIT", 3], ["OFFSET", 0]]
  ↳ app/views/admin/posts/index.html.erb:5
  Rendered admin/posts/index.html.erb within layouts/admin/application (Duration: 4.4ms | Allocations: 1640)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered partials/_navbar.html.erb (Duration: 0.9ms | Allocations: 909)
  Rendered partials/_footer.html.erb (Duration: 0.2ms | Allocations: 75)
Completed 200 OK in 28ms (Views: 15.6ms | ActiveRecord: 1.5ms | Allocations: 14931)

Searching image

ruby-on-rails ruby-on-rails-5
2个回答
1
投票

@@ Adwaith给了我解决方案。

local: true标签的[form_with确实对我有用,如下。

<%= form_with url: admin_posts_path, local: true, method: "get" do %>
  <%= text_field_tag :search, params[:search], id: "input-search", placeholder: "Search Posts" %>
<% end %>

感谢@Adwaith


0
投票

您尝试使用代替

© www.soinside.com 2019 - 2024. All rights reserved.