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

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

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

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

Ruby on Rails 6

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

ルーティング

ルーティングとは、TCP/IPネットワークにおいて、目的のホストまでパケットを送る為のパス選定のプロセスを言います。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

1251閲覧

Rails6 投稿の編集・更新でRouting Error

MIMY

総合スコア3

Ruby

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

Ruby on Rails 6

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

ルーティング

ルーティングとは、TCP/IPネットワークにおいて、目的のホストまでパケットを送る為のパス選定のプロセスを言います。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2021/05/01 13:54

編集2021/05/01 13:58

前提・実現したいこと

初心者です。
Ruby on Rails6でログインしたユーザーが新規投稿でき、自分の投稿は編集・削除ができるアプリを作っています。
アプリのGitHub
一通り実装後、InstagramやTwitterのように、タグから関連投稿一覧の表示をできるようにしたいと思い、
タグ付け機能を実装したところ、それ以前はできていた投稿編集でルーティングエラーメッセージが発生しました。

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

ActionController::RoutingError (No route matches [PATCH] "/topics"):

調べたこと

routes.rbを確認しました。
topics doで問題ないとは思うのですが、解決方法がわかりません。

Ruby

1Rails.application.routes.draw do 2 devise_for :users, controllers: { 3 invitations: 'users/invitations' 4 } 5 root to: 'topics#index' 6 resources :users, only: %i[edit update show] 7 resources :topics do 8 collection do 9 get 'all' 10 get 'about' 11 get 'search' 12 get 'tagsearch' 13 end 14 resources :comments, only: %i[create destroy] 15 end 16end

該当のソースコード

edit.html.erb

ruby

1<%= render "topics/header" %> 2<h4 class="mx-auto" style="max-width: 90%;"> 3 <span class="badge badge-warning">投稿内容の編集 - Edit Topic -</span> 4</h4> 5<%= render partial: "form", locals: { topic: @topic }%>

_form.html.erb

ruby

1<div class="card border-warning m-auto" style="max-width: 90%;"> 2 <%= form_with model: @topic, url: topics_path, class:"card-body", local: true do |f|%> 3 <div class="form-group"> 4 <%= f.label :title, "タイトル" %> 5 <%= f.text_field :title, placeholder: "Topic title" ,class: "form-control"%> 6 </div> 7 <div class="form-group"> 8 <%= f.label :text, "本文" %> 9 <%= f.text_area :text, placeholder: "Dear Femiに投稿してみましょう", rows: "7", class: "form-control"%> 10 </div> 11 <div class="form-control-fileform-control-file"> 12 <%= f.label :image, "画像" %><br> 13 <%= f.file_field :image %> 14 <div id="image-list"></div> 15 </div> 16 <div class="form-group"> 17 <%= f.label :tagname, "タグ" %> 18 <%= f.text_field :tagname, placeholder: "「#」を先頭につけてください 例)#育休", class:"form-control" %> 19 </div> 20 <div id="search-result"> 21 </div> 22 <%= f.submit "投稿", class: "btn btn-warning" %> <%= link_to "戻る", :back ,class: "btn btn-warning" %> 23 <% end %> 24</div>

ruby

1class TopicsController < ApplicationController 2 before_action :set_topic, only: %i[show edit update destroy] 3 before_action :contributor_confirmation, only: %i[edit update destroy] 4 5 def index 6 @topics = Topic.limit(5).order(created_at: :desc) 7 end 8 9 def all 10 @topics = Topic.all.order(created_at: :desc) 11 end 12 13 def new 14 @topic = TopicsTag.new 15 end 16 17 def create 18 @topic = TopicsTag.new(topic_params) 19 if @topic.valid? 20 @topic.save 21 redirect_to root_path 22 else 23 render 'new' 24 end 25 end 26 27 def show 28 @comment = Comment.new 29 @comments = @topic.comments.includes(:user) 30 end 31 32 def edit; end 33 34 def update 35 if @topic.update_attributes(topic_params) 36 redirect_to topic_path(@topic) 37 else 38 render :edit 39 end 40 end 41 42 def destroy 43 if @topic.destroy 44 redirect_to root_path 45 else 46 redirect_to topic_path(@topic) 47 end 48 end 49 50 def about; end 51 52 def search 53 @topics = Topic.search(params[:keyword]) 54 end 55 56 def tagsearch 57 return nil if params[:keyword] == '' 58 59 tag = Tag.where(['tagname LIKE ?', "%#{params[:keyword]}%"]) 60 render json: { keyword: tag } 61 end 62 63 private 64 65 def topic_params 66 params.require(:topics_tag).permit(:title, :text, :image, :tagname).merge(user_id: current_user.id) 67 end 68 69 def set_topic 70 @topic = Topic.find(params[:id]) 71 end 72 73 def contributor_confirmation 74 redirect_to root_path unless current_user == @topic.user 75 end 76end

Ruby

1class TopicsTag 2 include ActiveModel::Model 3 attr_accessor :user_id, :title, :text, :tagname, :image 4 5 with_options presence: true do 6 validates :title 7 validates :text 8 end 9 10 def save 11 topic = Topic.create(user_id: user_id, title: title, text: text, image: image) 12 tag = Tag.where(tagname: tagname).first_or_initialize 13 tag.save 14 15 TopicTagRelation.create(topic_id: topic.id, tag_id: tag.id) 16 end 17end

お分かりになる方がいたら、ご教示お願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

_form.html.erb

<%= form_with model: @topic, url: topics_path, class:"card-body", local: true do |f|%>

url: topics_path,が不要

投稿2021/05/01 14:16

asm

総合スコア15147

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

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

MIMY

2021/05/02 08:15

ありがとうございます。 新規投稿の際に url: topics_path, がないとDid you mean?で聞かれてしまったので、renderを使わないことにしました。
asm

2021/05/02 09:40

def to_model Topic.new(user_id: user_id, title: title, text: text, image: image) end がTopicsTagクラスに必要ですね
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問