LoginSignup
Yuya8888
@Yuya8888 (裕也)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

We're sorry, but something went wrong.でハマりました。

解決したいこと

会社で日々の日報投稿アプリをrailsで作成したのですが、herokuにデプロイしたところ、
それぞれの投稿の詳細が見れなくなってしまいましたので、見れるようにしたいです。

現在添付の画像の通り、トップページ・ユーザー詳細に関しては見れる状態です。
ただ、投稿の詳細をクリックすると『We're sorry, but something went wrong.』のエラーが出ます。

スクリーンショット 2021-07-16 10.45.15.png
スクリーンショット 2021-07-16 10.45.22.png
スクリーンショット 2021-07-16 10.45.29.png

発生している問題・エラー(heroku logs --tail)※ローカル環境では正常作動です。

2021-07-16T01:08:27.510149+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323] ActionView::Template::Error (undefined method `text6' for #<Prototype:0x000055f04b4ff740>
2021-07-16T01:08:27.510150+00:00 app[web.1]: Did you mean?  text5
2021-07-16T01:08:27.510150+00:00 app[web.1]: text4
2021-07-16T01:08:27.510151+00:00 app[web.1]: text2
2021-07-16T01:08:27.510151+00:00 app[web.1]: text3
2021-07-16T01:08:27.510151+00:00 app[web.1]: text1
2021-07-16T01:08:27.510152+00:00 app[web.1]: text5?
2021-07-16T01:08:27.510152+00:00 app[web.1]: text5=
2021-07-16T01:08:27.510153+00:00 app[web.1]: text4?
2021-07-16T01:08:27.510153+00:00 app[web.1]: text4=
2021-07-16T01:08:27.510154+00:00 app[web.1]: text3?
2021-07-16T01:08:27.510154+00:00 app[web.1]: text3=
2021-07-16T01:08:27.510155+00:00 app[web.1]: text2?
2021-07-16T01:08:27.510155+00:00 app[web.1]: text2=
2021-07-16T01:08:27.510155+00:00 app[web.1]: text1?
2021-07-16T01:08:27.510156+00:00 app[web.1]: text1=):
2021-07-16T01:08:27.510156+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323]     60:         <div class="prototype__detail">
2021-07-16T01:08:27.510157+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323]     61:           <p class="detail__title">本日の取り組み振り返り</p>
2021-07-16T01:08:27.510157+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323]     62:           <p class="detail__message">
2021-07-16T01:08:27.510158+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323]     63:             <%= @prototype.text6 %>
2021-07-16T01:08:27.510158+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323]     64:           </p>
2021-07-16T01:08:27.510159+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323]     65:         </div>
2021-07-16T01:08:27.510159+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323]     66:       </div>
2021-07-16T01:08:27.510159+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323]
2021-07-16T01:08:27.510160+00:00 app[web.1]: [4052942a-d2f8-49f4-a3c9-2fc57f9a7323] app/views/prototypes/show.html.erb:63

app/views/prototypes/_form.html.erb

<%= form_with model:prototype, class: 'form', local: true do |f| %>
  <div class="field">
    <%= f.label :title, "投稿日" %><br />
    <%= f.date_field :date %>
  </div>
  <div class="field">
    <%= f.label :catch_copy, "今月の目標と目的" %><br />
    <%= f.text_area :thema,:placeholder => "(例)それぞれ定めた今月の目標をここに毎回入力する。", class: :form__text_1 %>
  </div>

  <div class="koumoku">
    <%= "【本日の行動計画】" %><br />
  </div>

  <div class="field">
    <%= f.label :concept, "本日の重点課題と徹底する取り組み" %><br />
    <%= f.text_area :text1, :placeholder => "(例)本日は、スピアカフェのPVの撮影が終わったのでその動画編集をメインに、nutsのフィード投稿の編集を3投稿分実施。", class: :form__text %>
  </div>

  <div class="field">
    <%= f.label :concept, "本日の徹底する取り組みの目的" %><br />
    <%= f.text_area :text2, :placeholder => "(例)動画編集:YouTubeを検索しチャンネルに訪問してくれた方へのCM動画(自動再生される動画)として投稿し、
    より新規顧客獲得・オンラインサロン誘導に貢献するため。
    フィード投稿編集:なおさんに指示された内容を反映させながら、提案・修正案があればそれも反映させつつ、⚪︎月⚪︎日分の投稿分が必要なため実施する。", class: :form__text %>
  </div>

  <div class="field">
    <%= f.label :concept, "チーム全体の理念浸透➡成長・成果に繋げる取り組み" %><br />
    <%= f.text_area :text3,:placeholder => "(例)朝のMTでメンバーそれぞれの成功に対しての考えや意識の面が明確になり、
    メンバーそれぞれが、自分のタスクに関してどういう思いで向かい合ってるのかを考え仕事しているのが伝わっているので、
    それに対して自分はどうバックアップできるのか考え行動する。",  class: :form__text %>
  </div>

  <div class="field">
    <%= f.label :concept, "本日の行動指針に沿った取り組み" %><br />
    <%= f.text_area :text4, :placeholder => "(例)特に成長に関して意識して業務に当たる。
    朝早くにnutsに来て、業務時間ではできない読書をした。業務時間では、メンバーの業務に関して少しでもサポートできる部分はできるようにする。", class: :form__text %>
  </div>

  <div class="koumoku">
    <%= "【成果報告】" %><br />
  </div>

  <div class="field">
    <%= f.label :concept, "本日成長した点(チームの状況)" %><br />
    <%= f.text_area :text5, :placeholder => "(例)〇〇さんから教えて頂いた〇〇を即実践できたことが成長した点です。また前回〇〇だったのが〇〇になることができました!", class: :form__text %>
  </div>

  <div class="field">
    <%= f.label :concept, "本日の取り組み振り返り" %><br />
    <%= f.text_area :text6, :placeholder => "(例)本日は、動画編集と画像編集がメインだったが、効率化を図った上で早い時間で完成させ、他メンバーのタスクを協力することができた。", class: :form__text %>
  </div>

  <div class="actions">
    <%= f.submit "保存する", class: :form__btn  %>
  </div>
<% end %>

app/views/prototypes/show.html.erb

<main class="main">
  <div class="inner">
    <div class="prototype__wrapper">
      <p class="prototype__hedding">
        <%= @prototype.date %>
      </p>
      <%= link_to "by #{@prototype.user.name}","/users/#{@prototype.user.id}", class: :prototype__user %> 
      <% if user_signed_in? && current_user.id == @prototype.user_id %>
        <div class="prototype__manage">
          <%= link_to "編集する", edit_prototype_path(@prototype), class: :prototype__btn %>
          <%= link_to "削除する", prototype_path(@prototype), method: :delete, class: :prototype__btn %>
        </div>
      <% end %>
        <div class="prototype__body">
        <div class="prototype__detail">
          <p class="detail__title">今月の目標と目的</p>
          <p class="detail__message">
            <%= @prototype.thema %>
          </p>
        </div>
        <div class="koumoku">
          <%= "【本日の行動計画】" %><br />
        </div> 
        <div class="prototype__detail">
          <p class="detail__title">本日の重点課題と徹底する取り組み</p>
          <p class="detail__message">
            <%= @prototype.text1 %>
          </p>
        </div>
        <div class="prototype__detail">
          <p class="detail__title">本日の徹底する取り組みの目的</p>
          <p class="detail__message">
            <%= @prototype.text2 %>
          </p>
        </div>
        <div class="prototype__detail">
          <p class="detail__title">チーム全体の理念浸透➡成長・成果に繋げる取り組み</p>
          <p class="detail__message">
            <%= @prototype.text3 %>
          </p>
        </div>
        <div class="prototype__detail">
          <p class="detail__title">本日の行動指針に沿った取り組み</p>
          <p class="detail__message">
            <%= @prototype.text4 %>
          </p>
        </div>
        <div class="koumoku">
          <%= "【成果報告】" %><br />
        </div>
        <div class="prototype__detail">
          <p class="detail__title">本日成長した点(チームの状況)</p>
          <p class="detail__message">
            <%= @prototype.text5 %>
          </p>
        </div>
        <div class="prototype__detail">
          <p class="detail__title">本日の取り組み振り返り</p>
          <p class="detail__message">
            <%= @prototype.text6 %>
          </p>
        </div>
      </div>
      <div class="prototype__comments">
        <% if user_signed_in? %>
          <%= form_with(model: [@prototype, @comment], local: true) do |f|%>
            <div class="field">
                <%= f.label :text, "コメント" %><br />
                <%= f.text_field :text %>
            </div>
            <div class="actions">
              <%= f.submit "送信する", class: :form__btn  %>
            </div>
          <% end %> 
        <% end %>
        <ul class="comments_lists">
            <% if @comments %>
              <% @comments.each do |comment| %>
              <li class="comments_list">
              <%= comment.text %>
              <%= link_to  "(#{comment.user.name})", "/users/#{comment.user.id}", class: :comment_user %>
              <%= link_to  "削除", prototype_comment_path(@prototype,comment), method: :delete %>
              <% end %>
            <% end %>
            </li>
        </ul>
      </div>
    </div>
  </div>
</main>

app/controllers/prototypes_controller.rb

class PrototypesController < ApplicationController
  before_action :authenticate_user!, only: [:new,:edit,:destroy]
  # only ログインしてないと入れないページを指定

  def index
      @prototypes = Prototype.all.order(id: "DESC")
  end


  def new
    @prototype = Prototype.new
  end

  def create
    @prototype = Prototype.new(prototype_params)
    if @prototype.save
      redirect_to root_path
    else
      render :new  
    end
  end  

  def show
    #5人のメンバーはそれぞれの投稿は見れない。投稿しない社長・副社長に関しては5人のメンバー全員の投稿を見れる。
    @prototype = Prototype.find(params[:id])
    if current_user.id == @prototype.user.id || current_user.position == '代表取締役社長' ||  current_user.position == '代表取締役副社長' || (current_user.position == 'マネージャー1' && @prototype.user.name == ' 田中 利奈' ) || (current_user.position == 'マネージャー2' && @prototype.user.name == '堀田 惇貴' )
      @comment = Comment.new
      @comments = @prototype.comments.includes(:user)
    else
      redirect_to user_path(params[:user_id])
    end
  end

  def edit
    @prototype = Prototype.find(params[:id])
    unless @prototype.user_id == current_user.id
      redirect_to action: :index
    end
  end

  def update
    @prototype = Prototype.find(params[:id])
    if @prototype.update(prototype_params)
      redirect_to prototype_path
    else
      render :edit
    end
  end

  def destroy
    prototype = Prototype.find(params[:id])
    prototype.destroy
    redirect_to root_path
  end

  private

  def prototype_params
    params.require(:prototype).permit(:date, :thema, :text1,:text2,:text3,:text4,:text5,:text6).merge(user_id: current_user.id)
  end
end

app/models/prototype.rb

class Prototype < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy
  has_one_attached :image

  validates :date, presence: true 
  validates :thema, presence: true
  validates :text1, presence: true 
  validates :text2, presence: true
  validates :text3, presence: true
  validates :text4, presence: true
  validates :text5, presence: true
  validates :text6, presence: true
end

db/migrate/20210107065142_create_prototypes.rb

class CreatePrototypes < ActiveRecord::Migration[6.0]
  def change
    create_table :prototypes do |t|
      t.string :title,    null: false
      t.text :catch_copy, null: false
      t.text :concept,    null: false     
      t.references :user
      t.timestamps
    end
  end
end

db/migrate/20210518135619_remove_title_and_catch_copy_and_concept_from_prototypes.rb

class RemoveTitleAndCatchCopyAndConceptFromPrototypes < ActiveRecord::Migration[6.0]
  def change
    remove_column :prototypes, :title, :string, null: false
    remove_column :prototypes, :catch_copy, :text, null: false
    remove_column :prototypes, :concept, :text, null: false
  end
end

db/migrate/20210518135706_add_date_and_thema_and_text1_and_text2_and_text3_and_text4_and_text5_to_prototypes.rb

class AddDateAndThemaAndText1AndText2AndText3AndText4AndText5ToPrototypes < ActiveRecord::Migration[6.0]
  def change
    add_column :prototypes, :date, :date, null: false
    add_column :prototypes, :thema, :text, null: false
    add_column :prototypes, :text1, :text, null: false
    add_column :prototypes, :text2, :text, null: false
    add_column :prototypes, :text3, :text, null: false
    add_column :prototypes, :text4, :text, null: false
    add_column :prototypes, :text5, :text, null: false
    add_column :prototypes, :text6, :text, null: false
  end
end

自分で試したこと

元々カラムがtext5までしかなかったため、ロールバックしてtext6を追加してマイグレイトしました。
その後、『git push heroku master』し、『heroku run rails db:migrate』して、確認したところエラーになりました。
エラー内容しては、『ActionView::Template::Error (undefined method `text6' for #Prototype:0x000055f04b4ff740
ということなのですが、具体的な解決方法が調べてもわかりませんでした。
よろしくお願い致します。

0

1Answer

多分heroku run rails db:migrateが正常終了してないので、データベースにまだtext6というカラムが存在しないのではないかと。

もう一度

heroku run rails db:migrate

をやってみてエラー等表示されませんか?
また、

heroku run rails db:version

とやって表示されるバージョンはdb/migrate内の最後のファイル名と合ってますか?

0Like

Comments

  1. @Yuya8888

    Questioner
    回答ありがとうございます!!
    再度『heroku run rails db:migrate』を実施し、『heroku run rails db:version』を実施したところ、
    D, [2021-07-16T02:26:10.294781 #4] DEBUG -- : (7.3ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
    D, [2021-07-16T02:26:10.344790 #4] DEBUG -- : (2.6ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
    Current version: 20210110080529

    と表示されました。
    バージョンが合ってないみたいです。
    どうしたらよろしいのでしょうか?
  2. migrationがうまく行かないのはmigrationファイル内容に問題があるか、データベースのテーブルの実際の構成(カラムとかそのタイプとか色々)がmigration側と衝突しているとか色々考えられます。
    データを消すことができないのなら、heroku内のデータベース(postgresqlですよね?)の中に入って、該当のテーブルを走らせようとするmigrationの内容と衝突していないか確認して、衝突しているようなカラムがあるならそのカラムを消すとかなんとかする必要があります。

    多分問題は「カラムがtext5までしかなかったため、ロールバックしてtext6を追加して」のところで、普通ならtext6を追加したいならロールバックは必要なく、新規のmigrationファイルを作ってカラムを追加すべきだったと思います。
  3. @Yuya8888

    Questioner
    なるほどですね!!
    丁寧にありがとうございます!!
    最善策としては、リムーブして新しく『classAdd』として追加するべきでしょうか?
  4. 状況によるのでなんとも言えません。

    「migrationファイル内容に問題があるか、データベースのテーブルの実際の構成(カラムとかそのタイプとか色々)がmigration側と衝突しているとか色々考えられます。」

    捨てても良いデータであるなら、自分なら一旦データベースを消して最初からやり直します。

    rails db:drop
    rails db:create
    rails db:migrate
  5. @Yuya8888

    Questioner
    ありがとうございます!!!!
    実践してみます!

Your answer might help someone💌