前提
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/ツールのバージョンなど) 初学者です。情報に不手際あるかもしれませんが何卒よろしくお願い致します。
Prototype に title がない、と言われているのですから、
Prototype の スキーマ載せていただかないと、
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
こちらで宜しいでしょうか。
返信遅くなり申し訳ありません。
回答1件
あなたの回答
tips
プレビュー