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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Q&A

解決済

1回答

1063閲覧

[Rails6]Routingで想定しないアクションが呼ばれてしまう

jackson

総合スコア2

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/03/27 10:07

編集2021/03/27 10:16

##解決したいこと
railsでQAサイトを作っていますがroutes.rbでルーティングが想定しないアクションを呼んでしまいます。
具体的にはareaアクションを呼び出したいのにshowアクションが呼ばれます。

##起きているエラー
設定しているURLquestions/areaにリクエストすると以下のエラーがでます。

ActiveRecord::RecordNotFound in QuestionsController#show Couldn't find Question with 'id'=area

ここでshowアクションが呼ばれてしまっており、もちろんそれを想定していないのでエラーが起こってしまいます
しかもrails routesを実行してもしっかりルートはできています

% rails routes questions_area GET /questions/area(.:format) questions#area

##関連するコード
######route.rb

Rails.application.routes.draw do devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) resources :reactions get 'answers' => 'answers#index' resources :questions, shallow: true do resources :answers, shallow: true do resources :reactions end end resources :questions do resource :evaluation, only: [:create, :destroy] end devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' , registrations: 'users/registrations', sessions: 'users/sessions' } get 'users/show', to: 'users#show' resources :users resource :user, except: [:new, :create, :destroy] get 'questions/area' => 'questions#area' root 'questions#index' get 'pages/index' get 'pages/show' get 'static_pages/home' end

######app/controllers/questions_controller.rb

class QuestionsController < ApplicationController before_action :set_question, only: %i[ show edit update destroy] before_action :set_tag, only:[:new,:edit,:create,:update] before_action :authenticate_user!, only:[:new,:create,:edit,:update] def area @countries = Country.all end # GET /questions or /questions.json def index #@questions = current_user.questions.all @q = Question.ransack(params[:q]) @questions = @q.result(distinct: true) end # GET /questions/1 or /questions/1.json def show # before_action :set_question end # GET /questions/new def new @question = Question.new # before_action :set_tag end # GET /questions/1/edit def edit # before_action :set_question # before_action :set_tag if @question.user.id != current_user.id redirect_to root_url , alert: '不正なアクセスです' end end # POST /questions or /questions.json def create #before_action :set_tag @question = current_user.questions.build(question_params) respond_to do |format| if @question.save format.html { redirect_to @question, notice: "質問を投稿しました" } format.json { render :show, status: :created, location: @question } else format.html { render :new, status: :unprocessable_entity } format.json { render json: @question.errors, status: :unprocessable_entity } end end end # PATCH/PUT /questions/1 or /questions/1.json def update # before_action :set_question #before_action :set_tag respond_to do |format| if @question.update(question_params) format.html { redirect_to @question, notice: "質問を更新しました" } format.json { render :show, status: :ok, location: @question } else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @question.errors, status: :unprocessable_entity } end end end # DELETE /questions/1 or /questions/1.json def destroy # before_action :set_question @question.destroy respond_to do |format| format.html { redirect_to questions_url, notice: "質問を削除しました" } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_question @question = Question.find(params[:id]) end def set_tag @tags = Tag.all end def correct_user @user = User.find(params[:id]) if @user != current_user redirect_to root_url , alert: '不正なアクセスです' end end # Only allow a list of trusted parameters through. def question_params params.require(:question).permit(:user_id, :title, :body, :best_answer_id, {:tag_ids => []},:country_id) end end

######app/views/questions/area.html.erb

<% @counries.each do |country|%> <div class="row"> <div class="col-md-1"> <a href ="#" class="thumbnail"> <%= image_tag country.image ,class: "country" if @country.image? %> </div> </div>

以上何か足りないことがあれば追記致しますのでよろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

自己解決

route.rbのget 'questions/area' => 'questions#area'を resources :questionsより上に記述すれば通りました

投稿2021/03/27 11:24

jackson

総合スコア2

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問