前提・実現したいこと
Rubyで投稿アプリを作成しています。
Railsを使用しています。
errorsメソッドとバリデーションを駆使して、投稿失敗時や編集失敗時にエラーメッセージを表示したい。
発生している問題・エラーメッセージ
バリデーションに引っかかって投稿失敗時や編集失敗時にエラーメッセージを表示したいのですが、何もエラーが表示されなくて困っています。
赤い画面のエラーは現状出ておりません。
発生している問題は2点です。
①空の投稿は出来ないようにバリデーションがかかっているはずなのに、空の新規投稿が完了してホームに戻ってしまう。(データベースのデータが増えておらず、投稿数は増えないにも関わらず)条件分岐が機能していない?
②投稿済みの編集画面で、指定した文字数超過して更新をかけてもエラー文字が表示されない。
(バリデーションの文字数制限の関係で編集前の状態には戻ってくれる)
該当のソースコード
tweets_contoroller.rb↓
tweets_contoroller.rb
1class TweetsController < ApplicationController 2before_action :set_tweet, only: [:edit, :show] 3 4 def index 5 @tweets = Tweet.includes(:user).order("created_at DESC").all.page(params[:page]).per(8) 6 end 7 8 def new 9 @tweet = Tweet.new 10 end 11 12 def create 13 if Tweet.create(tweet_params) 14 flash[:notice] = "投稿が完了しました" 15 redirect_to root_path 16 else 17 flash.now[:alert] = 'メッセージを入力してください。' 18 # render :new 19 end 20 end 21 22 def destroy 23 tweet = Tweet.find(params[:id]) 24 tweet.destroy 25 flash[:notice] = "投稿を削除しました" 26 redirect_to root_path 27 end 28 29 def edit 30 end 31 32 def update 33 tweet = Tweet.find(params[:id]) 34 tweet.update(tweet_params) 35 36 if tweet.save 37 flash[:notice] = "更新が完了しました" 38 redirect_to root_path 39 else 40 redirect_to("/tweets/#{tweet.id}/edit") 41 end 42 end 43 44 def show 45 @comment = Comment.new 46 @comments = @tweet.comments.includes(:user) 47 end 48 49 def search 50 @tweets = Tweet.search(params[:keyword]) 51 respond_to do |format| 52 format.html 53 format.json 54 end 55 end 56 57 private 58 def tweet_params 59 params.require(:tweet).permit(:image, :text, :genre_id).merge(user_id: current_user.id) 60 end 61 62 def set_tweet 63 @tweet = Tweet.find(params[:id]) 64 end 65 66 def move_to_index 67 unless user_signed_in? 68 redirect_to action: :index 69 end 70 end 71end 72
ソースコード
tweets.rb(models)↓
tweets.rb
1class Tweet < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 belongs_to_active_hash :genre 4 belongs_to :user 5 has_many :comments 6 7 #空の投稿を保存できないようにする 8 validates :text, {presence: true, length: {maximum: 70}} 9 validates :image, :genre_id, presence: true 10 11 mount_uploader :image, ImageUploader 12 13 def self.search(search) 14 if search 15 Tweet.where('text LIKE(?)', "%#{search}%") 16 else 17 Tweet.all 18 end 19 end 20 21end 22
new_html.erb↓
<div class="contents row"> <div class="container"> <% @tweet.errors.full_messages.each do |message| %> <div class="form-error"> <%= message %> </div> <% end %> <%= form_with(model: @tweet, local: true) do |form| %> <h3>投稿する</h3> <%= render partial: "form", locals: { form: form } %> <% end %> </div> </div> <%= render partial: "footer" %>
edit.html.erb↓
<div class="contents row"> <div class="container"> <%= form_with(model: @tweet, local: true) do |form| %> <h3>編集する</h3> <% @tweet.errors.full_messages.each do |message| %> <div class="form-error"> <%= message %> </div> <% end %> <%= render partial: "form", locals: { form: form } %> <% end %> </div> </div> <%= render partial: "footer" %>
一応routes.rb↓
Rails.application.routes.draw do devise_for :users devise_scope :user do post 'users/guest_sign_in', to: 'users/sessions#new_guest' end root to: 'tweets#index' resources :tweets do resources :comments, only: :create collection do get 'search' end end resources :users, only: :show end
試したこと
①フラッシュメッセージの記述に間違いがあるのか?や、各アクション内での記述をrenderで書いていないからダメなのか複数試してみましたが効果なしです。赤い画面も出ないので余計に分かりません。
② rails consoleでエラー文のデータ保存を試そうとするが、戻り値でtrueが出ません。
rails consoleで実行した事(順に実行)
tweet = Tweet.new
tweet.saveでfalse
tweet.content = "任意の文字列"
tweet.saveで、またfalse
falseが出るので、どこかの記述がおかしいと疑っても、どこか見当がつかずにいます。
インスタンス変数になっているからダメなのかと仮説を立ててコード変更して試したものの、解決策にたどり着けておりません。
諸先輩方の知識をお借りしたく思います。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー