Home>

Sinatra wants to save and display the text in the form in db
(I was able to display it before)

Error message

When I post from HTML to/ideas with form, I get this error

ActiveRecord :: AssociationTypeMismatch at/ideas
User (# 70210205361440) expected, got "" which is an instance of String (# 70210191539740)
Applicable source code
enable: sessions
configure: production do
end
helpers do
  def current_user
    User.find_by (id: session [: user])
  end
end
get '/ ideas' do
  @ideas = Idea.all
  @users = User.all
  @favorites = Idea.where (favorite: true)
  erb: ideas
end
post '/ ideas' do
  current_user.ideas.create (
    description: params [: description],
    about: params [: about],
    concept: params [: concept],
    name: params [: name],
    core: params [: core] .inspect,
    sab: params [: sab] .inspect,
    technology: params [: technology] .inspect,
    age: params [: age],
    gender: params [: gender],
    user: params [: user],
    whom: params [: whom],
    when: params [: when],
    where: params [: where],
    why: params [: why],
    how: params [: how],
    howmany: params [: howmany],
    howmuch: params [: howmuch],
    howlong: params [: howlong],
    last: params [: last],
    servicename: params [: servicename] .inspect,
    servicedifference: params [: servicedifference],
    email: params [: email],
    date: params [: date],
    other: params [: other]
  )
  redirect '/ ideas'
end
require 'bundler/setup'
Bundler.require
if development?
  ActiveRecord :: Base.establish_connection (ENV ['DATABASE_URL'] || "sqlite3: db/development.db")
end
class User<ActiveRecord :: Base
  has_secure_password
  validates: username,
    presence: true
  validates: mail,
    presence: true,
    format: {with:/\ A [\ w + \-.] + @ [a-z \ d \-.] + \. [a-z] + \ z/i}
  validates: password,
    presence: true,
    length: {in: 4..20}
  has_many: ideas
  validates: mail, uniqueness: true
end
class Idea<ActiveRecord :: Base
  belongs_to: user
end
ActiveRecord :: Schema.define (version: 2018_08_12_012742) do
  create_table "ideas", force:: cascade do | t |
    t.string "description"
    t.text "about"
    t.string "concept"
    t.string "name"
    t.string "core"
    t.string "sab"
    t.string "technology"
    t.string "age"
    t.string "gender"
    t.string "user"
    t.string "whom"
    t.string "when"
    t.string "where"
    t.string "why"
    t.string "how"
    t.string "howmany"
    t.string "howmuch"
    t.string "howlong"
    t.string "last"
    t.string "servicename"
    t.string "servicedifference"
    t.text "email"
    t.string "date"
    t.text "other"
    t.text "comment"
    t.string "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "favorite", default: false
  end
  create_table "users", force:: cascade do | t |
    t.string "mail"
    t.string "username"
    t.string "password_digest"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
end
<! DOCTYPE html>
<html lang = "en">
<head>
    <meta charset = "UTF-8">
  <meta name = "viewport" content = "width = device-width, initial-scale = 1">
    <title>Idea Form</title>
</head>
<body onload = "addSample3 ();addSample2 ();addSample1 ();addSample ();date ();">
<% if! current_user.nil?%>
  <div>
    <div>
      <div>
        <div><span></span>Consolidate ideas</div>
        <div>
          <form action = "/ ideas" method = "post"></form>
          <div>
            <label for = "description"><h5>What kind of site/app? (Required)</h5></label><br>
            <input required type = "text" name = "description" placeholder = "site where you can do it." form = "ideaform">
          </div><br>

          <input type = "submit" form = "ideaform">
          <input type = "reset" form = "ideaform">
        </div>
      </div>
    </div>
  </div>
<% end%>
</body>
</html>

Rebuild db

Supplemental information (FW/tool version etc.)

ruby ​​2.5.1
rbenv 1.1.1

  • Answer # 1

    A combination ofbelongs_to: userandt.string"user"is strange.
    ActiveRecord expects an instance ofUserfor.user, butparams [: user]The direct cause is that an instance of/code>was passed.

    Because you are using ActiveRecord, you should understand the part related to ActiveRecord from the official guide.

    For example, the link below is about association.

    belongs_to: userbehaves as if an attribute calleduseris defined, but on the DB side, a BIGINT (old code) calleduser_idIf activerecord, INT) column is assumed, and this is used for saving.