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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

535閲覧

searchアクションに送る値がshowアクションに送られる

souda-takeru

総合スコア4

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/12/09 01:24

イベント投稿アプリを作っているのですが、searchアクションに送りたいparamsがshowアクションに送られエラーを起こしてしまいます。

エラー

ActiveRecord::RecordNotFound in EventsController#show Couldn't find Event with 'id'=search Extracted source (around line #44): def show @event = Event.find(params[:id]) @comment = Comment.new @comments = @event.comments.includes(:user) end ```### 参考コード ルーティング ```ここに言語を入力 Rails.application.routes.draw do devise_for :users root to: "events#index" resources :events, only: [:new, :create, :show, :destroy, :edit, :update] do resources :comments, only: :create member do post "add", to: "favorites#create" get "show_favorites" => "favorites#show_favorites" end collection do get 'search_tag' end end resources :users, only: :show resources :favorites, only: [:destroy, :index] get 'events/search' end ```イベントコントローラー ```ここに言語を入力 class EventsController < ApplicationController before_action :authenticate_user!,only: [ :edit, :destroy, :new, :create] before_action :search_event, only: [:index, :search] before_action :move_to_index, except: [:index, :show, :search_tag] def index @events = Event.all  end def search @results = @p.result #binding.pry end def search_tag @events = Event.search(params[:keyword]) end def show @event = Event.find(params[:id]) @comment = Comment.new @comments = @event.comments.includes(:user) end private def event_params params.require(:event).permit(:name, :explanation, :facility_id, :scale_id, :category_id, :volunteer, images: []).merge(user_id: current_user.id) end def tag_params params.require(:event).permit(:tag_names) end def search_event @p = Event.ransack(params[:q]) end ```indexビュー ```ここに言語を入力 <%= search_form_for @p, url: events_search_path do |f| %> <%= f.collection_select(:facility_id_eq, Facility.all, :id, :name, {}, {class:"select-box"}) %> <%= f.collection_select(:scale_id_eq, Scale.all, :id, :name, {}, {class:"select-box"}) %> <%= f.collection_select(:category_id_eq, Category.all, :id, :name, {}, {class:"select-box"}) %> <%= f.submit "検索", class: "search-btn" %> <% end %> ```seaechビュー ```ここに言語を入力 <% if @results.length !=0 %> <% @results.each do |result| %> <li> <%= link_to result.name, event_path(result.id) %> </li> <% end %> <% else %> 該当するイベントはありません <% end %> ```### 行った事 rails routesでindexビューの検索先のurlを確認するも events_search GET /events/search(.:format) events#search とsearchアクションを指定していましたがなぜかshowアクションにparamsが送られてしまいます。どこに原因があるかわかりません。アドバイスお願いします

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

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

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

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

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

guest

回答1

0

自己解決

ルーティングの記載が間違えたいました。
Rails.application.routes.draw do
devise_for :users
root to: "events#index"
resources :events, only: [:new, :create, :show, :destroy, :edit, :update] do
resources :comments, only: :create
member do
post "add", to: "favorites#create"
get "show_favorites" => "favorites#show_favorites"
end
collection do
get 'search_tag'
end
end
resources :users, only: :show
resources :favorites, only: [:destroy, :index]
get 'events/search'
end
から
Rails.application.routes.draw do
devise_for :users
root to: "events#index"
get 'events/search'
とget 'events/search'をroot to: "events#index"としました

投稿2020/12/09 01:55

souda-takeru

総合スコア4

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問