LoginSignup
0
0

More than 3 years have passed since last update.

【Ruby on Rails】 refileを使用した投稿一覧ページの作成

Last updated at Posted at 2020-12-10

目標

rifileをviewで表示させる

開発環境

ruby '2.6.5'
rails '6.0.0'

実装

前回の続き【refileを使用した新規投稿】

今回も、投稿機能(post)を例として作成します。

投稿を表示

app/views/posts/index.html.erb
  <% @posts.each do |post| %>
    <%= attachment_image_tag(post, :post_image) %>
    <%= post.title %>
  <% end %>

image_tagの先頭にattachmentをつける
ここでも_idは付けない

ページを読み込んでエラーを吐き出させる

スクリーンショット 2020-12-10 17.55.24.png

Refile.secret_keyをコピーする。(リロードするたびに数字が変わるので最新のkeyを使う)

Refile.secret_keyを貼り付ける

config/initializers/application_controller_renderer.rb
# 一番下に貼り付ける

  Refile.secret_key = '386bc3830a5cd265ca2af501b60846655cd6d0097e2b0f817e4491dce8d9a643fb7f339ce427b8849e61342fd28f0e6c9d9c9045415ec6d1b8a3a58770f04d3b'

その後に、サーバーを再起動する。

以上で実装できます。

クラス、パスを付けたい場合

<%= link_to attachment_image_tag(post, :post_image, class: "クラス名"), パス %>
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0