@Ogw17_08 (小川 祐汰)

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!

【データベースに保存できません】

解決したいこと

Ruby on Railsで画像投稿アプリを作成しています。
フォームからテキストをデータベースへ保存したいのですが、テーブルに保存することができません。
解決方法を教えていただけませんか。

発生している問題・エラー画像

エラー.png

index.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>ProtoSpace</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    <link href="https://fonts.googleapis.com/css?family=Noto+Sans+JP:400,700,900&display=swap" rel="stylesheet">
  </head>

  <body>
    <header class="header">
      <div class="inner">
        <div class="nav">
          <div class="nav__left">
          <h1>Cook Lists</h1>
            <%# <%= link_to image_tag("logo.png", class: :logo), root_path %> 
          </div>
          <%# ログインしているときは以下を表示するようにしましょう %>
          <% if user_signed_in? %>

            <div class="nav__right"> 
              <%="#{current_user.nickname}"%>
              <%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: :nav__logout %>
              <%= link_to "New Photo", new_photo_path, class: :nav__btn %>
              <%= link_to "Lists", root_path,class: :nav__btn %>
            </div>
          <% else %> 
          <%# // ログインしているときは上記を表示するようにしましょう %>
          <%# ログインしていないときは以下を表示するようにしましょう %>
            <div class="nav__right">
              <%= link_to "ログイン", new_user_session_path, class: :nav__btn %>
              <%= link_to "新規登録", new_user_registration_path, class: :nav__btn %>
              <%= link_to "Lists", root_path,class: :nav__btn %>
            </div>
          <% end %>
          <%# // ログインしていないときは上記を表示するようにしましょう %>
        </div>
      </div>
    </header>


    <h1>MEMO Lists</h1>
    <%= form_with model: @message, url: messages_path, class: 'form', local: true do |f| %>
      <div class="contents">
        <form class="contents-form">
          <%= f.text_area :memo, placeholder:"#", class:"lists-form" %>
        <div class="memo">
        </div>
        </form>
      </div>
      <div class="form_btn">
      <%= f.submit "送信", class:"form-submit"%>
      </div>
    <% end %>


      <%# <%= image_tag photo.image, class: 'photo-image'if photo.image.attached? %> 
    <%= yield %>
    <footer class="footer">
      <p class="copyright">Copyright © Cook Lists All rights reserved.</p>
    </footer>
  </body>
</html>
class MessagesController < ApplicationController

  def index
    @messages = Message.all
    @message = Message.new
  end

  def create
    @message = Message.new(message_params)
    if @message.save
      redirect_to root_path
    end
  end

  private

  def message_params
    params.permit(:memo).merge(user_id: current_user.id)
  end
end
Rails.application.routes.draw do
  devise_for :users
  root to: "photos#index"
  resources :photos, only: [:index, :new, :create, :show, :edit, :update, :destroy]
  resources :messages, only: [:index, :create]
end
class Message < ApplicationRecord
  belongs_to :user
end
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  validates :nickname, presence: true 
  has_many :photos
  has_many :messages
end
class CreateMessages < ActiveRecord::Migration[6.0]
  def change
    create_table :messages do |t|

      t.references :user, foreign_key: true
      t.string :memo
      t.timestamps
    end
  end
end

自分で試したこと

messagesコントローラー内のrequire(:message)を削除した。記述しても変化なし。
messagesコントローラー内のクリエイトアクションの確認。
userモデルとmessageモデルにアソシエーションが組めているか確認した。
マイグレーションファイルにmemoカラムが存在するかを確認した。
form_with内にurl指定。

0 likes

1Answer

このログがフォーム送信後を含んでいるのなら、

  1. form投稿のデータ内容が表示されてない(多分思っているコントローラーと表示してるコントローラーが違うとか?)
  2. PhotosController#indexしか表示してない(思っているのはMessagesController?)
  3. だが、多分Messageを保存しようとしている?

そもそもindex.html.erbはどこに保存されていますか?(もしかしてlayouts?)
普通Railsではlayoutsに全体に適用されるHTMLを書いて、各コントローラー毎、アクション毎にテンプレートがあるはずなんで、1つのファイルindex.html.erbだけで

  • 全体のレイアウト表示
  • 一覧表示
  • 新規作成フォーム表示

などは一緒に表現しないと思います。

def message_params
  params.permit(:memo).merge(user_id: current_user.id)
end

ここもmessageを保存しようとしているんで、permit(:message)に普通はすると思います。

全体的に基本から読み直す必要がありそうです。

1Like

Your answer might help someone💌