dockerを使ったrailsアプリケーションを作っていて、そこに画像投稿機能を実装しようと思っているのですがエラーになってしまいました
active strageを使って実装しようと思ったのですがうまくいきません
##ためしたこと
gem 'image_processing'
を入れてみました。
登校一覧画面に<td><%= image_tag user.image %></td>
を追加しました
##現状どんなかんじか
##該当のソースコード
いか、投稿一覧画面
<h2>ALL POST!</h2> <%= link_to 'もう一回行っちゃう?wwww', new_post_path %> <table> <tr> <th>title</th> <th>text</th> <th colspan="3"></th> </tr> <% @posts.each do |post| %> <tr> <td><%= post.title %></td> <td><%= post.body %></td> <td><%= post.image %></td> </tr> <% end %> </table>
いか、投稿フォーム
<%= form_with model: @post, local: true do |form| %> <p> <%= form.label :タイトル %><br> <%= form.text_field :title %> </p> <p> <%= form.label :内容 %><br> <%= form.text_area :body %> </p> <p> <td><%= form.file_field :image %></td> </p> <p> <td><input type="submit" value="投稿"></td> </p> <% end %>
以下、posts_controller.rb
class PostsController < ApplicationController def index @posts = Post.all end def new @post = Post.new end def create @post = Post.new(post_params) @post.save redirect_to action: 'index' end private def post_params params.require(:post).permit(:title, :body) end end
いか、comment_controller.rb
class CommentsController < ApplicationController def new @comment = Comment.new end def create @comment = Comment.create params.require(:comment).permit(:content, :image) # POINT redirect_to @comment end def show @comment = Comment.find(params[:id]) end def edit @comment = Comment.find(params[:id]) end def update @comment = Comment.find(params[:id]) @comment.update params.require(:comment).permit(:content, :image) # POINT @Comment.avatar.attach(params[:avatar]) redirect_to @commtnt end end
gemfile
1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.6.5' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 7gem 'rails', '~> 5.2.2' 8# Use mysql as the database for Active Record 9gem 'mysql2', '>= 0.4.4', '< 0.6.0' 10# Use Puma as the app server 11gem 'puma', '~> 3.11' 12# Use SCSS for stylesheets 13gem 'sass-rails', '~> 5.0' 14# Use Uglifier as compressor for JavaScript assets 15gem 'uglifier', '>= 1.3.0' 16# See https://github.com/rails/execjs#readme for more supported runtimes 17# gem 'mini_racer', platforms: :ruby 18gem 'jquery-rails' 19 20# Use CoffeeScript for .coffee assets and views 21gem 'coffee-rails', '~> 4.2' 22# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 23gem 'turbolinks', '~> 5' 24# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 25gem 'jbuilder', '~> 2.5' 26# Use Redis adapter to run Action Cable in production 27# gem 'redis', '~> 4.0' 28# Use ActiveModel has_secure_password 29# gem 'bcrypt', '~> 3.1.7' 30 31# Use ActiveStorage variant 32 gem 'mini_magick', '~> 4.8' 33 gem 'image_processing' 34# Use Capistrano for deployment 35# gem 'capistrano-rails', group: :development 36 37# Reduces boot times through caching; required in config/boot.rb 38gem 'bootsnap', '>= 1.1.0', require: false 39 40group :development, :test do 41 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 42 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 43 gem 'devise' 44 gem 'carrierwave' 45 gem 'rmagick' 46end 47 48group :development do 49 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 50 gem 'web-console', '>= 3.3.0' 51 gem 'listen', '>= 3.0.5', '< 3.2' 52 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 53 gem 'spring' 54 gem 'spring-watcher-listen', '~> 2.0.0' 55end 56 57group :test do 58 # Adds support for Capybara system testing and selenium driver 59 gem 'capybara', '>= 2.15' 60 gem 'selenium-webdriver' 61 # Easy installation and use of chromedriver to run system tests with Chrome 62 gem 'chromedriver-helper' 63end 64 65# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 66gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 67
進展がありました<td><%= post.image %></td>
にかえたところ、
以下の画像のようになりました
##参考にした記事
【Rails 5.2】 Active Storageの使い方
新規投稿機能の実装
##環境
OS:MacOS Big sur
ruby :ruby '2.6.5'
rails :gem 'rails', '~> 5.2.2'
Docker :20.10.0
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/26 00:45