前提・実現したいこと
ここに質問の内容を詳しく書いてください。
new.html.erbに画像投稿機能を反映させたいのですがエラーが出てしまいます。
<%= f.attachment_field :image %>
発生している問題・エラーメッセージ
```undefined local variable or method `f' for #<#Class:0x00007f9251736c58:0x00007f9251735268>
Extracted source (around line #6):
4<%= form_with(model: @tweet, local: true) do |form| %>
5 <h4>画像</h4>
6 <%= f.attachment_field :image %>ここがエラー
7 <%= form.text_area :text, placeholder: "本文", rows: "5" %>
8 <%= form.submit "送信" %>
9 <% end %>
エラーメッセージ
```undefined local variable or method `f' for #<#Class:0x00007f9251736c58:0x00007f9251735268>
<%= f.attachment_field :image %>
###ファイル
gemfile
ruby
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', '~> 6.0.0' 8# Use mysql as the database for Active Record 9gem 'mysql2', '0.5.3' 10# Use Puma as the app server 11gem 'puma', '~> 3.11' 12# Use SCSS for stylesheets 13gem 'sass-rails', '~> 5' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 4.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Redis adapter to run Action Cable in production 21# gem 'redis', '~> 4.0' 22# Use Active Model has_secure_password 23# gem 'bcrypt', '~> 3.1.7' 24 25# Use Active Storage variant 26# gem 'image_processing', '~> 1.2' 27 28# Reduces boot times through caching; required in config/boot.rb 29gem 'bootsnap', '>= 1.4.2', require: false 30 31group :development, :test do 32 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 33 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 34end 35 36group :development do 37 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 38 gem 'web-console', '>= 3.3.0' 39 gem 'listen', '>= 3.0.5', '< 3.2' 40 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 41 gem 'spring' 42 gem 'spring-watcher-listen', '~> 2.0.0' 43end 44 45group :test do 46 # Adds support for Capybara system testing and selenium driver 47 gem 'capybara', '>= 2.15' 48 gem 'selenium-webdriver' 49 # Easy installation and use of web drivers to run system tests with browsers 50 gem 'webdrivers' 51end 52 53# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 54gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 55 56gem 'pry-rails' 57gem 'devise' 58gem "refile", require: "refile/rails", github: 'manfe/refile' 59gem "refile-mini_magick"
scheme.rb
ActiveRecord::Schema.define(version: 2021_12_23_035607) do create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.string "text" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "image_id" end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.string "nickname" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end
tweet.rb
class Tweet < ApplicationRecord validates :text, presence: true attachment:image end
tweets_controller.rb
class TweetsController < ApplicationController before_action :set_tweet, only: [:edit, :show] before_action :move_to_index, except: [:index, :show] def index @tweets = Tweet.all end def new @tweet = Tweet.new end def create Tweet.create(tweet_params) end def destroy tweet = Tweet.find(params[:id]) tweet.destroy end def edit end def update tweet = Tweet.find(params[:id]) tweet.update(tweet_params) end def show end private def tweet_params params.require(:tweet).permit(:name, :text,:image) end def set_tweet @tweet = Tweet.find(params[:id]) end def move_to_index unless user_signed_in? redirect_to action: :index end end end
new.html.erb
<div class="contents row"> <div class="container"> <h3>投稿する</h3> <%= form_with(model: @tweet, local: true) do |form| %> <h4>画像</h4> <%= f.attachment_field :image %> <%= form.text_area :text, placeholder: "本文", rows: "5" %> <%= form.submit "送信" %> <% end %> </div> </div>
2021?????_add_image_id_to_tweets.rb
class AddImageIdToTweets < ActiveRecord::Migration[6.0] def change add_column :tweets, :image, :string end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。