前提・実現したいこと
railsでクイズ機能を実装しているのですが、Parametersを確認するとquestion_idしか含まれていないので
NoMethodErrorと出てしまいます。
どうしたら、choose_idも含めることができますか?
question_idが問題、choose_idはchoiceテーブルの選択肢のカラムです。
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
class ChoicesController < ApplicationController before_action :authenticate_user! def index @questions = Question.find_by(id: params[:question_id]) @choice = Choice.new end def create @choice = Choice.new(choice_params) if @choice.save redirect_to question_choices_path (@choice.question.id) else @question = @choice.question @questions = @question.choices render "questions/show" end end private def choice_params params.require(:choice).permit(:choose_id).merge(user_id: current_user.id, question_id: params[:question_id]) end end
class QuestionsController < ApplicationController before_action :authenticate_user!, except: [:index, :show] before_action :set_question, only: [:show, :edit, :update, :destroy] def index @question = Question.includes(:user).order('created_at DESC') end def new @question = Question.new end def create @question = Question.create(question_params) if @question.save redirect_to questions_path else render :new end end def edit redirect_to action: :index unless @question.user.id == current_user.id end def show @choice = Choice.new(@choice) end def update if @question.update(question_params) redirect_to question_path else render :new end end def destroy if current_user.id == @question.user.id @question.destroy redirect_to questions_path else render :new end end private def question_params params.permit(:problem, :answerd_id, :image, :commentary, :category_id).merge(user_id: current_user.id) end def set_question @question = Question.find(params[:id]) end end
questions.html.erb <%= render "devise/shared/header" %> <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3"> <div class="col"> <div class="card shadow-sm"> <%= "問."%><%= @question.problem %> <div> </div> <p> <div class="d-flex justify-content-between align-items-center"> <%= form_with model: [@question,@choice], local: true do |f|%> <div class="btn-group"> <%= f.collection_select(:choose_id, Choose.all, :id, :name, {}, {class:"btn btn-sm btn-outline-secondary", id:"category"}) %> <%= f.submit '答える', class:'btn btn-sm btn-outline-secondary' %> </div> <% end %> <% if user_signed_in? && current_user.id == @question.user.id %> <div class="btn-group"> <%= link_to '編集', edit_question_path, method: :get, class: "btn btn-sm btn-outline-secondary" %> <%= link_to '削除', question_path, method: :delete, class:'btn btn-sm btn-outline-secondary' %> </div> <% end %> <%= "カテゴリー:"%><%= @question.category.name %> </div> <div> </div> </div> </div> </div>
class Choose < ActiveHash::Base self.data = [ { id: 1, name: '選んでください。' }, { id: 2, name: '○' }, { id: 3, name: '×' }, ] include ActiveHash::Associations has_many :questions end
class Choice < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions has_many :users belongs_to :question belongs_to :choose with_options numericality: { other_than: 1, message: 'Select' }, presence: true do validates :choose_id end end
試したこと
ビューの設定に誤りがないか確認。
モデルの定義に問題がありそうです。code載せてください。
回答1件
あなたの回答
tips
プレビュー