Rails:pagy 忽略 attr_accessor

Beu*_*Beu 6 ruby-on-rails pagy

我需要在索引页上显示 attr_accessor 值。对于分页,我使用 pagey gem。下面是代码块:

def index
    @posts = Post.all
    @posts.map do |post|
      post_info = get_post_info(post.url)
      post.update_attribute(:icon, post_info["icon"])
      post.update_attribute(:subtitle, post_info["subtitle"])
    end
    print @posts[0].icon
    @pagy, @posts = pagy(@posts)    
  end
Run Code Online (Sandbox Code Playgroud)

第一篇文章的打印图标在控制器中提供了正确的 url。

当我post.icon在视图文件中使用时,它为零。如果我直接发送@posts而不使用pagey,它会按预期工作。怎么解决这个问题呢?

这是没有 pagey 的代码

def index
    @posts = Post.all
    @posts.map do |post|
      post_info = get_post_info(post.url)
      post.update_attribute(:icon, post_info["icon"])
      post.update_attribute(:subtitle, post_info["subtitle"])
    end
  end
Run Code Online (Sandbox Code Playgroud)

这是我的视图代码:

索引.html.erb

<div class="">
      <%= render partial: "posts/index", collection: @posts, as: :post %>
  </div> 
Run Code Online (Sandbox Code Playgroud)

_index.html.erb如下

  <div class="flex mb-4">
    <div class="rounded float-left mr-3 my-3" style=";">
      <% if !post.icon.nil? %>
        <%= image_tag post.icon, class: "app-icon float-right w-full h-full rounded-3xl", style: "width: 150px;" %>
      <% else %>
         <%= image_tag 'post.svg', class: "app-icon w-full h-full", style: "width: 150px;" %>
      <% end %>
    </div>
    <div style="width: 85%" class="mt-5 pt-3 lg:ml-4">
      <h3 class="lg:text-xl text-sm font-semibold text-gray-50"><%= post.title %></h3>
      <div class="lg:text-base text-xs text-gray-50"> <%= post.subtitle %> </div>
    </div>
  </div>
Run Code Online (Sandbox Code Playgroud)