@papu_1590

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

[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

2Answer

なんとなく.mergeしてる部分が問題なのかと

  def create
    binding.pry # コメント外す
    @tweets = Tweet.create(tweet_params)
  end

としておいて、

  1. 止まった箇所でparams.require(:tweet).permit(:image, :text)としてみてどういう値が出ますか?
  2. 止まった箇所でparams.require(:tweet).permit(:image, :text).merge(user_id: current_user.id)としてみてどういう値が出ますか?
  3. 止まった箇所でparams.require(:tweet).permit(:image, :text).to_h.merge(user_id: current_user.id)としてみてどういう値が出ますか?
0Like

Comments

  1. @papu_1590

    Questioner

    回答ありがとうございます!早速試してみました!
    1. [1] pry(#<TweetsController>)> params.require(:tweet).permit(:image, :text)
    => <ActionController::Parameters {"image"=>"sample1.png", "text"=>"hhh"} permitted: true>

    2. [2] pry(#<TweetsController>)> params.require(:tweet).permit(:image, :text).merge(user_id: current_user.id)
    => <ActionController::Parameters {"image"=>"sample1.png", "text"=>"hhh", "user_id"=>1} permitted: true>

    3. params.require(:tweet).permit(:image, :text).to_h.merge(user_id: current_user.id)
    => {"image"=>"sample1.png", "text"=>"hhh", "user_id"=>1}

    自分で確認したところ変な部分はなさそうでわからなかったんですがどうでしょうか?
    よろしくお願いいたします。

form_withmultipart: trueが無いからとか?
(デフォルトではf.file_fieldなどとあると勝手に入れてくれるっぽいですが...)

もしくはerbがおかしい。<%= form_withの位置がズレていたので直してます。

<div class="tweets-contents">
  <%= form_with model: @tweets, url: tweets_path, local: true do |f| %>

    <div class="tweets-main">
      <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>
0Like

Comments

  1. @papu_1590

    Questioner

    回答ありがとうございます!
    multipart: trueのこと知らなかったので自分でも調べてみました!
    修正し試しに実行したところエラー文はかわりませんでした。

Your answer might help someone💌