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

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

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

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

Ruby

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

Q&A

解決済

1回答

360閲覧

ruby on rails 編集後のルーティングでエラーが出てしまう

kousuke24

総合スコア29

Ruby on Rails 5

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

Ruby

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

0グッド

0クリップ

投稿2019/08/21 12:20

現在ruby on railsでWebアプリケーションを作成しています。テキストを編集してupdateでredirect_toでエラーが出てしまいます。

エラー

Routing Error Routes match in priority from top to bottom

ルーティング

Rails.application.routes.draw do root to: 'toppages#index' resources :sessions, only: [:new, :create, :destroy] resources :users, only: [:index, :show, :new, :create] resources :novels, only: [:new, :edit, :update, :create, :destroy] end

コントローラー

class NovelsController < ApplicationController before_action :require_user_logged_in before_action :correct_user, only: [:destroy] def new @novel = Novel.new end def edit @novel = Novel.find(params[:id]) end def update @novel = Novel.find(params[:id]) if @novel.update(novel_params) flash[:success] = '小説は正常に更新されました' redirect_to @novel else flash.now[:danger] = '小説は更新されませんでした' render :edit end end def create @novel = current_user.novels.build(novel_params) if @novel.save flash[:success] = '小説を保存しました' redirect_to root_url else @novels = current_user.novels.order(id: :desc).page(params[:page]) flash.now[:danger] = '小説の投稿に失敗しました' render 'toppages/index' end end def destroy @novel.destroy flash[:success] = '小説を削除しました' redirect_back(fallback_location: root_path) end private def novel_params params.require(:novel).permit(:title, :content) end def correct_user @novel = current_user.novels.find_by(id: params[:id]) unless @novel redirect_to root_url end end end

ビュー

<% novels.each do |novel| %> <%= novel.title %> <%= novel.content %> <% if current_user == novel.user %> <%= link_to 'この小説を編集する', edit_novel_path(novel) %> <%= link_to "削除", novel, method: :delete, data: { confirm: "本当に削除してもよろしいでしょうか?" }, class: 'btn btn-danger btn-sm' %> <% end %> <% end %> <%= paginate novels %>

ルーティングエラーが出てしまいます。泣き崩れそうです。ご教授いただければ幸いです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

redirect_to @novel

novels#showがありませんけど
どこに飛ぶことを意図していますか?

投稿2019/08/21 12:37

asm

総合スコア15147

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

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

kousuke24

2019/08/21 12:46

users#indexに飛ばしたいのですが redirect_to users_path で飛ばせませんでした。NovelのコントローラーからUserに飛ばすことはできないのでしょうか?
asm

2019/08/21 13:08

redirect_to users_path で大丈夫な筈です。 redirect_to controller: 'users', action: :index を試すか、 $ rails routes コマンドの結果をご確認ください
kousuke24

2019/08/21 13:12

user_path←Routing Errorでした redirect_to controller: 'users', action: :index←これもRouting Error rails routes ``` Prefix Verb URI Pattern Controller#Action root GET / toppages#index sessions POST /sessions(.:format) sessions#create new_session GET /sessions/new(.:format) sessions#new session DELETE /sessions/:id(.:format) sessions#destroy users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new user GET /users/:id(.:format) users#show novels POST /novels(.:format) novels#create new_novel GET /novels/new(.:format) novels#new edit_novel GET /novels/:id/edit(.:format) novels#edit novel PATCH /novels/:id(.:format) novels#update PUT /novels/:id(.:format) novels#update DELETE /novels/:id(.:format) novels#destroy ``` pathは間違っていないのですが、エラーになってしまいます、、、
kousuke24

2019/08/21 13:56

users_urlでやったら正常に作動しました! ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問