前提・実現したいこと
Sinatraでformに入ってる文章をdbに保存して表示させたい
(前まで表示できていた)
発生している問題・エラーメッセージ
HTMLから、formで/ideasにpostしたところこのようなエラーが起きてしまう
ActiveRecord::AssociationTypeMismatch at /ideas User(#70210205361440) expected, got "" which is an instance of String(#70210191539740)
該当のソースコード
ruby
1 2enable :sessions 3 4configure :production do 5end 6 7helpers do 8 def current_user 9 User.find_by(id: session[:user]) 10 end 11end 12 13get '/ideas' do 14 @ideas = Idea.all 15 @users = User.all 16 @favorites = Idea.where(favorite: true) 17 erb :ideas 18end 19 20post '/ideas' do 21 current_user.ideas.create( 22 description: params[:description], 23 about: params[:about], 24 concept: params[:concept], 25 name: params[:name], 26 core: params[:core].inspect, 27 sab: params[:sab].inspect, 28 technology: params[:technology].inspect, 29 30 age: params[:age], 31 gender: params[:gender], 32 user: params[:user], 33 34 whom: params[:whom], 35 when: params[:when], 36 where: params[:where], 37 why: params[:why], 38 how: params[:how], 39 howmany: params[:howmany], 40 howmuch: params[:howmuch], 41 howlong: params[:howlong], 42 last: params[:last], 43 servicename: params[:servicename].inspect, 44 servicedifference: params[:servicedifference], 45 email: params[:email], 46 date: params[:date], 47 other: params[:other] 48 ) 49 redirect '/ideas' 50end 51 52
models
1require 'bundler/setup' 2Bundler.require 3 4if development? 5 ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || "sqlite3:db/development.db") 6end 7 8class User < ActiveRecord::Base 9 has_secure_password 10 validates :username, 11 presence: true 12 validates :mail, 13 presence: true, 14 format: {with:/\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i} 15 validates :password, 16 presence: true, 17 length: {in: 4..20} 18 19 has_many :ideas 20 validates :mail, uniqueness: true 21end 22 23class Idea < ActiveRecord::Base 24 belongs_to :user 25end 26 27
schema
1 2 3ActiveRecord::Schema.define(version: 2018_08_12_012742) do 4 5 create_table "ideas", force: :cascade do |t| 6 t.string "description" 7 t.text "about" 8 t.string "concept" 9 t.string "name" 10 t.string "core" 11 t.string "sab" 12 t.string "technology" 13 t.string "age" 14 t.string "gender" 15 t.string "user" 16 t.string "whom" 17 t.string "when" 18 t.string "where" 19 t.string "why" 20 t.string "how" 21 t.string "howmany" 22 t.string "howmuch" 23 t.string "howlong" 24 t.string "last" 25 t.string "servicename" 26 t.string "servicedifference" 27 t.text "email" 28 t.string "date" 29 t.text "other" 30 t.text "comment" 31 t.string "user_id" 32 t.datetime "created_at", null: false 33 t.datetime "updated_at", null: false 34 t.boolean "favorite", default: false 35 end 36 37 create_table "users", force: :cascade do |t| 38 t.string "mail" 39 t.string "username" 40 t.string "password_digest" 41 t.datetime "created_at", null: false 42 t.datetime "updated_at", null: false 43 end 44 45end 46
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width,initial-scale=1"> 6 <title>アイデアフォーム</title> 7</head> 8 9<body onload="addSample3();addSample2();addSample1();addSample();date();"> 10<% if !current_user.nil? %> 11 <div class="container"> 12 <div class="row"> 13 <div class="card" style="width: 1200px;"> 14 <div class="card-header" style="background: #10a0e0; 15 color: #fff;"><span class="glyphicon-glyphicon-triangle-right"></span>アイデアをまとめる</div> 16 <div class="card-body"> 17 <form action="/ideas" method="post" id="ideaform"></form> 18 <div class="form-group"> 19 <label for="description"><h5>どんなサイト・アプリ?(必須)</h5></label><br> 20 <input required type="text" name="description" id="description" placeholder="〇〇できるサイト。" form="ideaform" class="border border-danger" style="width: 50%;"> 21 </div><br> 22 23 24 <input type="submit" class="btn btn-info" form="ideaform"> 25 <input type="reset" class="btn btn-outline-danger" form="ideaform"> 26 </div> 27 </div> 28 </div> 29 </div> 30<% end %> 31</body> 32 33</html>
試したこと
dbの再構築
補足情報(FW/ツールのバージョンなど)
ruby 2.5.1
rbenv 1.1.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。