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

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

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

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

タグ

特殊な記法により文書に埋め込む形で記述される付加情報をタグと呼びます。文書構造や書式、文字飾りなどを指示したり、画像や他の文書へのリンクを埋め込むことができる。

Ruby on Rails

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

Q&A

解決済

1回答

2144閲覧

Rails6でインスタやTwitterのようなハッシュタグ検索を実装したい

MIMY

総合スコア3

Ruby on Rails 6

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

タグ

特殊な記法により文書に埋め込む形で記述される付加情報をタグと呼びます。文書構造や書式、文字飾りなどを指示したり、画像や他の文書へのリンクを埋め込むことができる。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/05/02 09:29

初心者で、初めてオリジナルのアプリを作成しています。
github

ハッシュタグ検索→関連トピックの一覧表示を実装したい

Rails6で、ハッシュタグの便利な機能を実装したいと考えています。

参考にした記事①
参考にした記事②

見よう見まねで実装中に以下のエラーメッセージが発生しました。

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

詳細ページに遷移しようとした時

NoMethodError in Topics#show undefined method `gsub' for #<Tag::ActiveRecord_Associations_CollectionProxy:0x00007fae28d01a50> Extracted source (around line #3):

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

http://localhost:3000/topics/tagと直接打ち込んだ時

Couldn't find Topic with 'id'=tag

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

http://localhost:3000/topics/tag/testと直接打ち込んだ時

Routing Error No route matches [GET] "/topics/tag/test"

該当のソースコード

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 get '/topic/tag/:name', to: "topics#tag" 14 end 15 resources :comments, only: %i[create destroy] 16 end 17end

Ruby

1module TopicsHelper 2 def render_with_tags(tagname) 3 tagname.gsub(/[##][\w\p{Han}ぁ-ヶヲ-゚ー]+/){|word| link_to word, "/topic/tag/#{word.delete("#")}"}.html_safe 4 end 5end

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(topics_tag_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 def tag 64 @user = current_user 65 @tag = Tag.find_by(tagname: params[:name]) 66 @topics = @tag.topics 67 end 68 69 private 70 71 def topics_tag_params 72 params.require(:topics_tag).permit(:title, :text, :image, :tagname).merge(user_id: current_user.id) 73 end 74 75 def topic_params 76 params.require(:topic).permit(:title, :text, :image).merge(user_id: current_user.id) 77 end 78 79 def set_topic 80 @topic = Topic.find(params[:id]) 81 end 82 83 def contributor_confirmation 84 redirect_to root_path unless current_user == @topic.user 85 end 86end

Ruby

1<%= render "topics/header" %> 2<div class="card border-light mb-8" style="mx-auto;"> 3 <div class="card-body"> 4 <div class="card-header"> 5 <h5><%= @topic.title %></h5> 6 </div> 7 <%= image_tag @topic.image.variant(resize: '400'), class: 'd-block user-select-none' if @topic.image.attached?%> 8 <p class="card-text"><%= @topic.text %></p> 9 <% @topic.tags.each do |tag| %> 10 <%= tag.tagname%> 11 <% end %> 12 <%= render_with_tags(@topic.tags) %> 13 <h6 class="text-muted">by <%= link_to @topic.user.name, user_path(@topic.user) %> 14 <%= @topic.created_at %></h6> 15 <% if current_user == @topic.user%> 16 <%= link_to "編集する", edit_topic_path(@topic), class: "badge badge-warning" %> 17 <%= link_to "削除する", topic_path(@topic), method: :delete, class: "badge badge-warning" %> 18 <% end %> 19 </div> 20</div>

Ruby

1class Topic < ApplicationRecord 2 belongs_to :user 3 has_one_attached :image 4 has_many :topic_tag_relations, dependent: :destroy 5 has_many :tags, through: :topic_tag_relations 6 has_many :comments, dependent: :destroy 7 accepts_nested_attributes_for :topic_tag_relations 8 9 validate :text, unless: :was_attached? 10 11 def was_attached? 12 image.attached? 13 end 14 15 def self.search(search) 16 if search != '' 17 Topic.where('text LIKE(?)', "%#{search}%") 18 else 19 Topic.all 20 end 21 end 22 23 after_create do 24 topic = Topic.find_by(id: self.id) 25 tags = self.tagname.scan(/[##][\w\p{Han}ぁ-ヶヲ-゚ー]+/) 26 tags.uniq.map do |tag| 27 tag = Tag.find_or_create_by(tagname: tag.downcase.delete('#')) 28 topic.tags << tag 29 end 30 end 31 32 before_update do 33 topic = Topic.find_by(id: self.id) 34 topic.tags.clear 35 tags = self.tagname.scan(/[##][\w\p{Han}ぁ-ヶヲ-゚ー]+/) 36 tags.uniq.map do |tag| 37 tag = Tag.find_or_create_by(tagname: tag.downcase.delete('#')) 38 topic.tags << tag 39 end 40 end 41end

お分かりになる方がいらっしゃいましたら、ご教示ください。
よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

発生している問題・エラーメッセージ①
render_with_tags(@topic.tags) で @topic.tags を渡してますが、tagsはTagのインスタンスではなく、その集合ですので、gsub はありません。

発生している問題・エラーメッセージ②
http://localhost:3000/topics/tag は showと解釈されるので,tagがidとみなされます

発生している問題・エラーメッセージ③
get '/topic/tag/:name tptopicは単数形、なのに http://localhost:3000/topics/tag/test と複数形で呼んでます。

とエラーの原因は明確なのですが、全体構成をどうしようとしているのか、がよくわからないので、これ以上の助言は今はできないです。

投稿2021/05/02 13:24

winterboum

総合スコア23284

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

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

MIMY

2021/05/05 12:56 編集

③凡ミスですね… 御回答ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問