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

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

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

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

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

Q&A

解決済

1回答

470閲覧

rubyのactive hashのデータの取得について

mac07

総合スコア5

Ruby

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

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

0グッド

0クリップ

投稿2022/07/11 05:03

rubyのactive hashを活用して地域のプルダウンを作成し、DB(mysql)の保存は確認できましたが、保存されたデータをviewファイルに表示しようとした際にエラーになります。

index.html.erb

<div class="contents row"> <% @posts.each do |post| %> <td> <%= post.mark %></td><br> <td> <%= post.content %></td><br> <td> <%= post.area1.name %></td><br> <td> <%= post.area2.name %></td><br> <% end %> </div>

post_controller

class PostsController < ApplicationController

before_action :authenticate_user!, only: [:new, :index]

def new
@post = Post.new
end

def index
@posts = Post.all
end

def create
post = Post.new(post_params)
if post.save
redirect_to root_path
else
render :new
end
end

private
def post_params
params.require(:post).permit(:image, :video, :area1_id, :area2_id, :mark, :injury_id, :escape_id, :help_id, :content).merge(user_id: current_user.id)
end
end

エラー文
NoMethodError in Posts#index
undefined method `area2' for #Post:0x00007f908c337858
Did you mean? area1
area1=

ちなみにarea2は存在してデータの保存もできています

<td> <%= post.mark %></td><br> <td> <%= post.content %></td><br> <td> <%= post.area1.name %></td><br> <td> <%= post.area2.name %></td><br>←こちら

ここの post.area2.name のところでエラーになります
ちなみにarea1.nameは一覧画面に表示されています。

原因が全く話からずなので、どなたか教えていただきたいです

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

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

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

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

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

winterboum

2022/07/11 11:56

model Post の schemaを載せて
mac07

2022/07/11 13:34

# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # This file is the source Rails uses to define your schema when running `bin/rails # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to # be faster and is potentially less error prone than running all of your # migrations from scratch. Old migrations may fail to apply correctly if those # migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2022_07_10_095014) do create_table "active_storage_attachments", charset: "utf8", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false t.bigint "record_id", null: false t.bigint "blob_id", null: false t.datetime "created_at", null: false t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true end create_table "active_storage_blobs", charset: "utf8", force: :cascade do |t| t.string "key", null: false t.string "filename", null: false t.string "content_type" t.text "metadata" t.string "service_name", null: false t.bigint "byte_size", null: false t.string "checksum", null: false t.datetime "created_at", null: false t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true end create_table "active_storage_variant_records", charset: "utf8", force: :cascade do |t| t.bigint "blob_id", null: false t.string "variation_digest", null: false t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end create_table "comments", charset: "utf8", force: :cascade do |t| t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "posts", charset: "utf8", force: :cascade do |t| t.bigint "user_id" t.binary "image" t.binary "video" t.integer "state_id", null: false t.integer "area_id", null: false t.text "mark" t.integer "injury_id", null: false t.integer "escape_id", null: false t.integer "help_id", null: false t.text "content", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["user_id"], name: "index_posts_on_user_id" end create_table "users", charset: "utf8", force: :cascade do |t| t.string "name", default: "", null: false t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.boolean "user_type" t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "posts", "users" end こちらになります。 ※なお、カラム名がarea1とarea2で似ていることが原因かなと思い カラム名を変更しています area1_id  → statu area2_id → area と変更しています。
winterboum

2022/07/12 00:18

途中でcodeなどを変えると、質問との関係がゴチャゴチャになり、回答できなくなります。 変更したなら、変更後の挙動、エラーメッセージを載せてください。 それから app/models/post.rb も必要になりました。
mac07

2022/07/13 03:38

大変申し訳ありません、以後気をつけます。 ●エラーメッセージ NoMethodError in Posts#index undefined method `area' for #<Post:0x00007fdf208943b8> Did you mean? area_id Extracted source (around line #21): 19 20 21 22 23 24 </div> <div class="post-box"> <td> <%= post.area.name %></td> </div> <div class="post-box"> <td> <%= post.injury.name %></td> エラーメッセージはNoMethodErrorで特に変わりはありません ●app/models/post.rb class Post < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :state, :area, :escape, :help, :injury validates :state_id, numericality: { other_than: 1, message: "can't be blank"} validates :area_id, numericality: { other_than: 1, message: "can't be blank"} validates :escape_id, numericality: { other_than: 1, message: "can't be blank"} validates :help_id, numericality: { other_than: 1, message: "can't be blank"} validates :injury_id, numericality: { other_than: 1, message: "can't be blank"} # belongs_to :comments belongs_to :user has_one_attached :image end こちらになります、よろしくお願いいたします
guest

回答1

0

ベストアンサー

belongs_to :state, :area, :escape, :help, :injury
これ、5行に分けて個別に定義して

投稿2022/07/13 04:54

winterboum

総合スコア23645

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

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

mac07

2022/07/13 05:37

エラーの改善することができました! とても助かりました!ありがとうございました! ご指摘いただきました質問の仕方等気をつけます^^;
winterboum

2022/07/13 06:38

>エラーメッセージはNoMethodErrorで特に変わりはありません ちがいます。 undefined method `area' for #<Post:0x00007fdf208943b8> Did you mean? area_id ここが変わっているはず エラーメッセージは1行だけではないです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問