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

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

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

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

Q&A

解決済

1回答

571閲覧

ActiveModel::UnknownAttributeError in PrototypesController#createのエラーを解決したい

TERIA41

総合スコア1

Ruby

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

0グッド

0クリップ

投稿2022/10/11 13:11

編集2022/10/11 14:30

前提

railsで某プログラミングスクールのアプリ実装課題に取り組んでおります
その中で新規投稿画面から保存をクリックするとこのようなエラーが発生しました

実現したいこと

フォーム記述後、保存できるようにしたいです。
試したこととしましてはprototypescontrollerのcreateメソッドに誤りがないか確認しました。

発生している問題・エラーメッセージ

ActiveModel::UnknownAttributeError in PrototypesController#create unknown attribute 'title' for Prototype.

該当のソースコード

route.rb Rails.application.routes.draw do devise_for :users root to: "prototypes#index" resources :prototypes, only: [:index, :new, :create, :show] end 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 コード ```_form.html.erb <%= form_with model: prototype, local: true do |f|%> <div class="field"> <%= f.label :title, "プロトタイプの名称" %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :catch_copy, "キャッチコピー" %><br /> <%= f.text_area :catch_copy, class: :form__text %> </div> <div class="field"> <%= f.label :concept, "コンセプト" %><br /> <%= f.text_area :concept, class: :form__text %> </div> <div class="field"> <%= f.label :image, "プロトタイプの画像" %><br /> <%= f.file_field :image %> </div> <div class="actions"> <%= f.submit "保存する", class: :form__btn %> </div> <% end %> 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 PrototypesControllerです。 class PrototypesController < ApplicationController def index @prototypes = Prototype.all end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path(@prototype) else render :new end end def show @prototype = Prototype.find(params[:id]) end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end end 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 prototype.rb class Prototype < ApplicationRecord belongs_to :user has_one_attached :image validates :title, presence: true validates :catch_copy, presence: true validates :concept, presence: true validates :image, presence: true end ### 補足情報(FW/ツールのバージョンなど) 初学者です。情報に不手際あるかもしれませんが何卒よろしくお願い致します。

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

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

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

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

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

winterboum

2022/10/11 14:41

Prototype に title がない、と言われているのですから、 Prototype の スキーマ載せていただかないと、
TERIA41

2022/10/12 10:53

create_prototype.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, null: false, foreign_key: true t.timestamps end end end ====================================- devise_create_user.rb # frozen_string_literal: true class DeviseCreateUsers < ActiveRecord::Migration[6.0] def change create_table :users do |t| ## Database authenticatable t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" t.string :name, null: false t.text :profile, null: false t.text :occupation, null: false t.text :position, null: false ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable # t.integer :sign_in_count, default: 0, null: false # t.datetime :current_sign_in_at # t.datetime :last_sign_in_at # t.string :current_sign_in_ip # t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps null: false end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end end こちらで宜しいでしょうか。 返信遅くなり申し訳ありません。
guest

回答1

0

ベストアンサー

DBのカラム設定ミスかもしれないのでmigrationファイルを見せていただければと思います。

投稿2022/10/12 02:44

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

TERIA41

2022/10/12 10:42

create_prototype.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, null: false, foreign_key: true t.timestamps end end end ====================================- devise_create_user.rb # frozen_string_literal: true class DeviseCreateUsers < ActiveRecord::Migration[6.0] def change create_table :users do |t| ## Database authenticatable t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" t.string :name, null: false t.text :profile, null: false t.text :occupation, null: false t.text :position, null: false ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable # t.integer :sign_in_count, default: 0, null: false # t.datetime :current_sign_in_at # t.datetime :last_sign_in_at # t.string :current_sign_in_ip # t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps null: false end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end end 確認お願い致します。 返信遅くなり申し訳ありません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問