前提・実現したいこと
こちらのサイトを参考に、nameを本のタイトル、syosaiを著者、kanryoを読了にするだけの簡単な変更のみして、図書管理のアプリケーションを作っています。
一度、タスク管理のアプリケーションは作っているのですが、以下のエラーコードがどう編集したらいいのかわかりません。
発生している問題・エラーメッセージ
SQLite3::SQLException: no such column: kanryo: SELECT "tosyos".* FROM "tosyos" ORDER BY kanryo asc, kigen asc LIMIT ? OFFSET ?
該当のソースコード
<h2>一覧画面</h2> <%= link_to "全て表示", tosyos_path %> <%= link_to "完了のみ", tosyos_path + "?kanryo=true", style:"margin-left:5px;" %> <%= link_to "未完了のみ", tosyos_path + "?kanryo=false", style:"margin-left:5px;" %> <%= link_to "タスクを登録", new_tosyo_path, class:"pull-right" %> <table class="table table-bordered"> <thead> <tr> <th class="grid-th-width1 pc-only">No</th> <th class="grid-th-width2">図書</th> <th class="grid-th-width3">期限</th> <th class="grid-th-width4">ステータス</th> <th class="grid-th-width5">編集</th> </tr> </thead> <tbody> <% @tosyos.each do |tosyo| %> <tr style="height:48px;"> <td class="pc-only"><%= @grid_no.to_s %></td> <td style="vertical-align: middle"><%= tosyo.name %></td> <td style="vertical-align: middle"><%= kigen_format(tosyo.kigen) %></td> <td style="vertical-align: middle"><%= kanryo_tag(tosyo) %></td> <td class="henshu-cell"> <%= link_to "照会", tosyo_path(tosyo) %> <%= link_to "編集", edit_tosyo_path(tosyo) %> <%= link_to "削除", tosyo_path(tosyo), method:"delete" %> </td> </tr> <% @grid_no = @grid_no + 1 %> <% end %> <tbody> </table> <div style="text-align: center"> <%= will_paginate @tosyos, renderer: BootstrapPagination::Rails, previous_label: "前へ", next_label: "次へ", inner_window: 1, outer_window: 0, class:"pagination-sm" %> </div>
【models/concerns/tosyo.rb】 class Tosyo < ApplicationRecord #期限のアクセサー(画面入力用) attr_accessor :kigen_str #kanryo(文字列)の値がある場合、絞り込みを行う scope :by_kanryo, ->(kanryo){ if kanryo.present? #bool型の変数 kanryo_bool = true if kanryo == "true" kanryo_bool = true else kanryo_bool = false end where('kanryo = ?', kanryo_bool) end } validate :check_name validate :check_shosai validate :check_kigen_str private #nameのバリデーション def check_name if !name.present? errors.add("図書","を入力してください") elsif name.length > 20 errors.add("図書","は20文字以内で入力してください") end end #shosaiのバリデーション def check_shosai if shosai.present? && shosai.length > 100 errors.add("著者","は100文字以内で入力してください") end end #kigen_strのバリデーション def check_kigen_str if kigen_str.present? && !is_yyyymmdd?(kigen_str) errors.add("期限","のフォーマットが不正です") end end #文字列がyyyyMMdd型であるか def is_yyyymmdd?(yyyymmdd) if !yyyymmdd.present? return false end if yyyymmdd.length != 8 return false end if !(yyyymmdd =~ /^[0-9]+$/) return false end result = true begin yyyy = yyyymmdd[0..3].to_i mm = yyyymmdd[4..5].to_i dd = yyyymmdd[6..7].to_i Date.new(yyyy,mm,dd) rescue result = false end result end end
【migrate/2018~…_create_tosyos.rb】 class CreateTosyos < ActiveRecord::Migration[5.1] def change create_table :tosyos do |t| t.string :name t.string :shosai t.datetime :kigen t.boolean :kanryo t.timestamps end end end
試したこと
ひとまず、Task、Tasks、task、tasks、@task、@tasksの部分をtosyo、tosyosといったように変更しています。
補足情報(FW/ツールのバージョンなど)
おそらく Ruby on Rails の最新のものが使えているかと思います。
Sqlite3を使っているのですが、itermでは動かすことは難しいのでしょうか?
現在、sqlite3には接続?できているのですが、それ以降の作業ができていません…。
モデルファイルやマイグレーションファイルはどのようになっておりますか?
ただいま、追加いたしました。
こちらのファイルでよろしかったでしょうか?
データベースのテーブルの情報って見れますか?完コピしたソースコードの、表示の部分だけ変えたんでしたっけ?
すみません、データベースのテーブルの情報のみかたがわからず…。教えていただけますと幸いです。
そうですね!実際に表示される部分だけ変えています。
https://qiita.com/knife0125/items/bb095a85d1a5d3c8f706 このページにわかりやすくまとめてあります!(^^)
ありがとうございます!
おそらく、Mac?rails?に入っている、sqlite3を使っているのですが、上記の通りにやってみてもテーブルの方にアクセスできません。