LoginSignup
onaka0_0suita
@onaka0_0suita (m- yuto)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

【Rails6】cocoonを使用してActiveHashのnameを取得できない。

解決したいこと

cocoonを使用して、動的フォームを追加/削除出来るよう実装しました。
しかし、動的フォームには、ActiveHashを使用しており、詳細ページにて、ActiveHashのidは取得できるのですが、nameが取得できない状況です。
解決策をご教授頂けますと幸いです。

-- 動的フォーム --
Image from Gyazo

エラー

ActionView::Template::Error (undefined method `item' for #<CoordinationItem:0x00007f8a86433510>
Did you mean?  item_id):
    85:           <table>
    86:             <% @coordination.coordination_items.each do |coordination| %>
    87:             <tr>
    88:               <th><%= coordination.item.name %>:</th>
    89:               <td><%= coordination.item_text %></td>
    90:             </tr>
    91:             <% end %>

該当するソースコード

データベース

coordinations.rb
class CreateCoordinations < ActiveRecord::Migration[6.0]
  def change
    create_table :coordinations do |t|
      t.string     :coordination_title, null: false
      t.text       :coordination_profile
      t.references :user, null: false, foreign_key: true
      t.timestamps
    end
  end
end
coordination_items.rb
class CreateCoordinationItems < ActiveRecord::Migration[6.0]
  def change
    create_table :coordination_items do |t|
      t.references :coordination, null: false, foreign_key: true
      t.integer    :item_id,      null: false
      t.string     :item_text,    null: false
      t.timestamps
    end
  end
end

モデル

coordination.rb
class Coordination < ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :user
  has_many :coordination_items, dependent: :destroy
  accepts_nested_attributes_for :coordination_items
  has_many :comments

  has_one_attached :image

  validates :coordination_title, presence: true
end
coordination_item.rb
class CoordinationItem < ApplicationRecord
  belongs_to_active_hash :item
  belongs_to :coordination

  with_options presence: true do
    validates :item_id
    validates :item_text
  end

  validates :item_id, numericality: { other_than: 1 }
end
item.rb
class Item < ActiveHash::Base
  self.data = [
    { id: 1, name: '--' },
    { id: 2, name: 'トップス' },
    { id: 3, name: 'ジャケット/アウター' },
   ...
    { id: 35, name: 'その他' }
  ]
end

コントローラー

coordinations_controller.rb
class CoordinationsController < ApplicationController
  before_action :set_coordination, only: %i[show edit update destroy]
  before_action :authenticate_user!, only: %i[new edit destroy]

  def new
    @coordination = Coordination.new
    @coordination_items = @coordination.coordination_items.new
  end

  def create
    @coordination = Coordination.create(coordination_params)
    if @coordination.save
      redirect_to root_path
    else
      render :new
    end
  end

  def show
    @comment = Comment.new
    @comments = @coordination.comments.includes(:user)
  end

  private

  def coordination_params
    params.require(:coordination).permit(:coordination_title, :image, :coordination_profile,
                                         coordination_items_attributes: %i[id item_id item_text _destroy]).merge(user_id: current_user.id)
  end

  def set_coordination
    @coordination = Coordination.find(params[:id])
  end
end

ビュー

show.html.erb
<table>
  <% @coordination.coordination_items.each do |coordination| %>
    <tr>
      <th><%= coordination.item.name %>:</th>
      <td><%= coordination.item_text %></td>
    </tr>
  <% end %>
</table>

自分で試したこと

ビューで<%= coordination.item_id %>とするとidは取得できるのですが、item.nameとすると上部のエラーが出てしまいます。
その他に、コントローラーのshowアクションで、Coordination_item.findと指定して、ビューに反映しても、エラーが出てしまいます。
上記のコードの他に必要なコードがありましたら、提示いたします。
よろしくお願いいたします。

0

1Answer

Comments

  1. @onaka0_0suita

    Questioner
    @CASIXx1 様
    ご回答下さり、ありがとうございます!
    いただいた内容で試した結果、無事解決することが出来ました!
    また、このような考え方は全く無かったので大変助かりました。
    本当にありがとうございます!

Your answer might help someone💌