[Rails] ActiveStorageを使った画像の保存でつまづいています
解決したいこと
簡単なsnsのアプリケーションを開発中の超初心者です。
投稿機能を実装しようとしてるのですが、ActiveStorageを使いツイッターのような画像と
一緒に文を投稿できるようにしたいです
発生している問題・エラー
下記はエラー画面の画像です。
https://i.gyazo.com/16315fb483b2e0e39987370b8ca83227.png
https://i.gyazo.com/be9cd088c8c3a902208bd698af3cb750.png
ActiveSupport::MessageVerifier::InvalidSignature at /tweets
ActiveSupport::MessageVerifier::InvalidSignature
該当するソースコード
def create
#binding.pry
@tweets = Tweet.create(tweet_params)
end
自分で試したこと
Strong Parametersが適切に設定できていないっぽいので確認してみましたがわかりませんでした。
tweetsコントローラーです
class TweetsController < ApplicationController
before_action :set_tweet, only: [:edit, :show, :update, :destroy]
before_action :move_to_index, except: [:index, :show, :search]
def index
@tweets = Tweet.includes(:user).order("created_at DESC")
end
def new
@tweet = Tweet.new
end
def create
#binding.pry
@tweets = Tweet.create(tweet_params)
end
def destroy
if @tweet.destroy
redirect_to root_path
end
end
def edit
end
def update
@tweet.update(tweet_params)
end
def show
@comment = Comment.new
@comments = @tweet.comments.includes(:user)
end
def search
@tweet = Tweet.search(params[:keyword])
end
private
def tweet_params
params.require(:tweet).permit(:image, :text).merge(user_id: current_user.id)
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
下記マイグレーションファイルになります
class CreateTweets < ActiveRecord::Migration[6.0]
def change
create_table :tweets do |t|
t.text :text, null: false
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
end
下記はtweetモデルです
belongs_to :user
has_one_attached :image
validates :text, presence: true
下記はroutesです
Rails.application.routes.draw do
devise_for :users
root 'tweets#index'
resources :users
resources :tweets do
resources :comments, only: [:create]
collection do
get 'search'
end
end
end
下記は投稿画面のviewです
<div class="tweets-contents">
<div class="tweets-main">
<%= form_with model: @tweets, url: tweets_path, local: true do |f| %>
<div class="img-upload">
<div class="weight-bold-text">
投稿画像
</div>
<div class="click-upload">
<p>
クリックしてファイルをアップロード
</p>
<%= f.file_field :image, id:"posts-image" %>
<div id="image-list"></div>
</div>
</div>
<div class="new-posts">
<div class="posts-explain">
<div class="weight-bold-text">
投稿内容
</div>
<%= f.text_area :text, class:"posts-text", id:"post-info", placeholder:"text" ,rows:"7" ,maxlength:"1000" %>
</div>
</div>
<%# 下部ボタン %>
<div class="sell-btn-contents">
<%= f.submit "投稿する" ,class:"sell-btn" %>
</div>
<%# /下部ボタン %>
</div>
<% end %>
</div>
0 likes