Jason KIM
Jason KIM

Reputation: 147

After rendering, browser doesn't refresh the page

I am currently stuck with the search function for blog post. Controller and html rendering seemed to work well, but I couldn't see any update from browser, Chrome.

From the start of searching scenario, it goes to _navbar.html.erb as below

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

, and it goes to posts_controller.rb as below

  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

I found the value of @posts got the right result by using puts through the server log. However, finally after rendering index.html.erb as below, browser doesn't update any rendered html at all. It just stayed as the current page.

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

When I saw the server log, it definitely rendered the search results, but I don't have any idea why the browser screen doesn't change anything.

This is the server log

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

Upvotes: 0

Views: 664

Answers (1)

Jason KIM
Jason KIM

Reputation: 147

@Adwaith gave me the solution.

local: true for the form_with tag really worked for me as below.

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

Thanks @Adwaith

Upvotes: 1

Related Questions