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

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

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

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

Q&A

解決済

1回答

389閲覧

railsno such column: tags.nameのエラーの解決方法について

yukitodesu3

総合スコア10

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/05/28 07:30

編集2021/05/28 09:57

cloud9を使い、ポートフォリオを作っているのですが、no such column: tags.name:が出ており、解決方法がわかりません。

新規投稿の際、タグ機能もつけて、データを保存するコードを書いたのですが、保存した後エラーが発生しました。
タグは複数保存できるように実装し,をつけることでtagのidを配列のして保存するといったものになっています。
投稿のやり方としては、モーデルを使い実装しています。

イメージ説明

#該当ソースコード

post.rb

class Post < ApplicationRecord belongs_to :profile has_many :post_item has_many :tag_maps, dependent: :destroy has_many :tags, through: :tag_maps accepts_nested_attributes_for :post_item def save_tag(post_tags) post_tags.each do |new_name| post_tag = Tag.find_or_create_by(name: new_name) self.tags << micropost_tag end end mount_uploaders :images, ImageUploader enum genre_name: { 寿司・魚料理: 0, 和食・日本料理: 1, ラーメン・麺類: 2, 丼もの・揚げ物: 3, お好み焼き・粉物: 4, 郷土料理: 5, アジア・エスニック: 6, 中華: 7, イタリアン: 8, 要職・西洋料理: 9, フレンチ: 10, アメリカ料理: 11, アフリカ料理: 12, 珍しい各国料理: 13, 焼肉・ステーキ: 14, 焼き鳥・串料理: 15, こだわり肉料理: 16, 鍋: 17, しゃぶしゃぶ・すき焼き: 18, 居酒屋・バー: 19, カフェ・スイーツ: 20, ファミレス・ファストフード: 21, ピッフェ・バイキング: 22} end

tag.rb

class Tag < ApplicationRecord has_many :tag_maps, dependent: :destroy, foreign_key: 'tag_id' has_many :posts, through: :tag_maps validates :name, uniqueness: true end

tagのマイグレーションファイル

class CreateTags < ActiveRecord::Migration[5.0] def change create_table :tags do |t| t.string :tag_name,null: false t.timestamps null: false end end end

tag_map.rb

class TagMap < ApplicationRecord belongs_to :post belongs_to :tag end

tag_mapのマイグレーションファイル

class CreateTagMaps < ActiveRecord::Migration[5.0] def change create_table :tag_maps do |t| t.references :post, foreign_key: true t.references :tag, foreign_key: true t.timestamps null: false end add_index :tag_maps, [:post_id,:tag_id],unique: true end end

post.contrlloer

class Public::PostsController < ApplicationController def index @profile = Profile.find(params[:profile_id]) @post = Post.new @posts = Post.all.page(params[:page]).per(10) @tag_list = Tag.all @post_tags = @post.tags end def create @profile = Profile.find(params[:profile_id]) @post = Post.new(posts_params) @post.profile_id = @profile.id tag_list = params[:post][:tag_ids].split(',') @post.save if @post.save_tag(tag_list) redirect_to public_profile_posts_path(@profile) else render index end end def show @profile = Profile.find(params[:profile_id]) @post = Post.find(params[:id]) end protected def posts_params params.require(:post).permit(:title, :genre_name,{images: []},:price,:introduction) end end

index.html.erb

<div class="container mt-5"> <div class="row"> <div class="col"> <% @tag_list.each do |list| %> <%= link_to list.tag_name, tag_posts_path(tag_id: list.id) %> <%= "(#{list.posts.count})" %> <% end %> <% post.tags.each do |tag| %> <%= tag.name %> <% end %> <table class= "table"> <h5>投稿一覧</h5> <thead> <tr> <th>タイトル</th> <th>ジャンル名</th> <th>料理説明</th> <th>料理説明</th> <th>値段</th> <th>料理詳細</th> </tr> </thead> <tbody> <% @posts.each do |post| %> <tr> <td><%= post.title %></td> <td><%= post.genre_name %></td> <td><%= post.price %></td> <td><%= post.introduction %></td> <td> <% post.images.each do |post| %> <%= image_tag (post.url),width: '30px', height: '30px' %> <% end %> </td> <td> <%= link_to '料理詳細へ', public_profile_post_path(@profile.id,post.id) ,class: "btn btn-success" %> </td> </tr> <% end %> </tbody> </table> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> 新規投稿 </button> <%= paginate @posts %> </div> </div> </div> <!-- Modal --> <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">新規投稿</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <%= form_for @post,url: public_profile_posts_path(@profile),local: true do |f| %> <div class="modal-body"> <div class="form-group"> <div class="m-2"> <%= f.label 'タイトル' %> <%= f.text_field :title %> </div> <div class="m-2"> <%= f.label 'ジャンル' %> <%= select :post, :genre_name, Post.genre_names.keys.to_a, include_blank: true %> </div> <div class="m-2"> <%= f.label '値段' %> <%= f.text_field :price %> </div> <div class="m-2"> <%= f.label '料理説明' %> <%=f.text_area :introduction, class: "padding:20px;" %> </div> <div class="m-2"> <%= f.label '料理画像' %> <%= f.file_field :images, multiple: true %> </div> <%= f.text_field :tag_ids, class: "form-control", id:'tag_ids',\ placeholder: "タグをつける。複数つけるには','で区切ってください。" %> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">閉じる</button> <%= f.submit '投稿する', class: "btn btn-primary" %> </div> <% end %> </div> </div> </div> </div>

#試したこと

イメージ説明

byebugを使い、tag_listの中身を見たところ

イメージ説明

string型として、データが送られていました。idとして所得しているので、string型でデータとして送られていることに問題があると思っていますが、どう修正したら良いか、わかりません。
またtag以外はデータとしてきちんと保存されていました。

お忙しいとは思いますが、回答の方よろしくお願いします。

post_tag = Tag.find_or_create_by(name: new_name)

post_tag = Tag.find_or_create_by(tag_name: new_name)

に変更したところ
イメージ説明

のエラーが発生しました。

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

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

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

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

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

guest

回答1

0

ベストアンサー

class CreateTags に
t.string :tag_name,null: false
とあります。
post_tag = Tag.find_or_create_by(name: new_name)

post_tag = Tag.find_or_create_by(tag_name: new_name)

投稿2021/05/28 09:43

winterboum

総合スコア23416

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

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

yukitodesu3

2021/05/28 09:55

修正したのですが、エラー文が変わり、新しいエラーが発生しました。
winterboum

2021/05/28 11:24

はて 再起動しても変わりませんか
yukitodesu3

2021/05/28 12:28

はい、エラー変わりません
winterboum

2021/05/28 13:26

Parameters のところ見せてください
yukitodesu3

2021/05/28 13:41

あの後、一度cloud9を開き直したところデータが治りました。 ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問