質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

922閲覧

バリデーションを解除できません

morfonica

総合スコア33

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

1グッド

1クリップ

投稿2021/09/29 06:10

前提・実現したいこと

投稿するときにカテゴリーを選択させるようにしたのですが、必要なくなったので、バリデーションを外したいのですが解除できません。

発生している問題・エラーメッセージ

ActiveRecord::RecordInvalid in Public::PostsController#create

バリデーションに失敗しました: Categoryを入力してください

post.emotions = "その他" end if post.save! redirect_to mypage_path, notice: '投稿しました!' else @categories = Category.all

該当のソースコード

controller

1class Public::PostsController < ApplicationController 2 3 before_action :authenticate_customer! 4 before_action :ensure_correct_customer, only: [:edit, :update, :destroy] 5 6 def index 7 # カテゴリー検索をしたら 8 if params[:emotions] 9 all_posts = Post.where(emotions: params[:emotions]) 10 else 11 all_posts = Post.includes(:category) 12 end 13 muted_customer = Relationship.where(muter_id: current_customer.id) 14 # 共感数が多い順 15 if params[:sort] == "sympathy" 16 @posts = all_posts.page(params[:page]).left_outer_joins(:sympathies).where.not(customer_id: muted_customer.pluck(:muted_id)). 17 group('posts.id').select('posts.*, COUNT("sympathies.*") AS sympathy').order('count(post_id) desc') 18 # 結合したテーブルをpost.idでグループ化する。共感されている対象のPostが同じ同士でグループ化する 19 # select文で返すデータを指定(postテーブルの全てとsympathies_count) 20 # pluck:activeモデルを継承していないと使えない。配列[]から指定したカラムを持ってくる。無いとnullになる。 21 # 応援数が多い順 22 elsif params[:sort] == "cheer" 23 @posts = all_posts.page(params[:page]).left_outer_joins(:cheers).where.not(customer_id: muted_customer.pluck(:muted_id)). 24 group('posts.id').select('posts.*, COUNT("cheers.*") AS cheer').order('count(post_id) desc') 25 else 26 @posts = all_posts.page(params[:page]).reverse_order.where.not(customer_id: muted_customer.pluck(:muted_id)) 27 end 28 @all_posts_count = all_posts.count 29 @categories = Category.all 30 end 31 32 def new 33 @post = Post.new 34 @categories = Category.all 35 end 36 37 def create 38 post = Post.new(post_params) 39 post.customer_id = current_customer.id 40 # API側から返ってきた値をもとにスコアを作成 41 post.score = Language.get_data(post_params[:text]) 42 # 数値をもとにタグ付け 43 if post.score > 0.3 44 post.emotions = "ポジティブ" 45 elsif post.score < -0.3 46 post.emotions = "ネガティブ" 47 else 48 post.emotions = "その他" 49 end 50 if post.save! 51 redirect_to mypage_path, notice: '投稿しました!' 52 else 53 @categories = Category.all 54 render :new 55 end 56 57 end 58 59 def edit 60 end 61 62 def update 63 post = Post.find(params[:id]) 64 post.score = Language.get_data(post_params[:text]) 65 if post.score > 0.3 66 post.emotions = "ポジティブ" 67 elsif post.score < -0.3 68 post.emotions = "ネガティブ" 69 else 70 post.emotions = "その他" 71 end 72 if post.update(post_params) 73 redirect_to mypage_path, notice: '投稿を更新しました!' 74 else 75 render :edit 76 end 77 end 78 79 def destroy 80 @post.destroy 81 redirect_to mypage_path, notice: '投稿を削除しました!' 82 end 83 84 def search 85 @posts = Post.where('text LIKE(?)', "%#{params[:keyword]}%") 86 render :index 87 end 88 89 private 90 91 def post_params 92 params.require(:post).permit(:text, :sympathies, :cheers, :image) 93 end 94 95 def ensure_correct_customer 96 @post = Post.find(params[:id]) 97 unless @post.customer == current_customer 98 redirect_to posts_path 99 end 100 end 101end 102

試したこと

postモデルのバリデーションはコメントアウトしました。

model

1 # validates :category, presence: true

ストロングパラメーターのcategory_idも消しました。

contoroller

1def post_params 2 params.require(:post).permit(:text, :sympathies, :cheers, :image) 3end

補足情報(FW/ツールのバージョンなど)

cloud9
rails version 5.2.6

alto_i0👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/09/29 06:32

rails再起動してみました?
guest

回答1

0

自己解決

postモデルの

belongs_to :category

の箇所をnillを許可できるように

belongs_to :category, optional: true

と変更しました。

投稿2021/09/29 11:04

morfonica

総合スコア33

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問