前提・実現したいこと
railsでtodoアプリを作っています。
未完了ボタン(押すと完了、再び押すと未完了に表示が変わる)を実装中で、
statusカラムを用いて、デフォルトに'undone'を入れ、ボタンが押されたら、'done'に変えるという形で実装使用としているのですが、データベースにデフォルト値が入りません。
発生している問題
ターミナル
sqlite> .headers on sqlite> select * from todos; id|content|limit|created_at|updated_at|status 3|c|2020-02-26|2020-02-26 11:57:53.716157|2020-02-26 11:57:53.716157| 4|qwe|2020-02-26|2020-02-26 12:02:10.998656|2020-02-26 12:02:10.998656| 5|z|2020-02-26|2020-02-26 13:44:00.001908|2020-02-26 13:44:00.001908| 6|za|2020-02-26|2020-02-26 13:45:34.297800|2020-02-26 13:45:34.297800| 7|qw|2020-02-26|2020-02-26 13:47:11.471128|2020-02-26 13:47:11.471128| 8|g|2020-02-26|2020-02-26 14:44:47.614557|2020-02-26 14:44:47.614557| 9|q|2020-02-26|2020-02-26 16:10:47.334061|2020-02-26 16:10:47.334061| 10|zz|2020-02-27|2020-02-27 02:03:48.355255|2020-02-27 02:03:48.355255|
該当のソースコード
db/migrate/20200225045522_create_todos.rb
class CreateTodos < ActiveRecord::Migration[5.1] def change create_table :todos do |t| t.text :content t.date :limit t.timestamps end end end
db/migrate/20200226100616_add_status_to_todos.rb
class AddStatusToTodos < ActiveRecord::Migration[5.1] def change add_column :todos, :status, :string, default: 'undone' end end
db/schema.rb
ActiveRecord::Schema.define(version: 20200226100616) do create_table "todos", force: :cascade do |t| t.text "content" t.date "limit" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "status" end end
routes.rb
Rails.application.routes.draw do get '/todos/search', to: 'todos#search' resources :todos # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root to: "todos#index" end
todos_controller.rb
class TodosController < ApplicationController before_action :set_todo, only: [:show, :edit, :update, :destroy] # GET /todos # GET /todos.json def index @todos = Todo.all.order(created_at: :desc)#新しい順 @todo = Todo.new end # GET /todos/1 # GET /todos/1.json def show end # GET /todos/new def new @todo = Todo.new end # GET /todos/1/edit def edit end # POST /todos # POST /todos.json def create @todo = Todo.new(todo_params) respond_to do |format| if @todo.save format.html { redirect_to todos_url(@todo), notice: 'Todo was successfully created.' } format.json { render :show, status: :ok, location: @todo } else format.html { render :new } format.json { render json: @todo.errors, status: :unprocessable_entity } end end end # PATCH/PUT /todos/1 # PATCH/PUT /todos/1.json def update respond_to do |format| if @todo.update(todo_params) format.html { redirect_to @todo, notice: 'Todo was successfully updated.' } format.json { render :show, status: :ok, location: @todo } else format.html { render :edit } format.json { render json: @todo.errors, status: :unprocessable_entity } end end end # DELETE /todos/1 # DELETE /todos/1.json def destroy @todo.destroy respond_to do |format| format.html { redirect_to todos_url, notice: 'Todo was successfully destroyed.' } format.json { head :no_content } end end def done#完了ボタン @todo.update(status: "Done") redirect_to todos_path end def search#検索 @todos = Todo.all.order(created_at: :desc)#新しい順 @todos2=@todos.where(['content LIKE ?', "%#{params[:search]}%"]) end private # Use callbacks to share common setup or constraints between actions. def set_todo @todo = Todo.find(params[:id]) end # Only allow a list of trusted parameters through. def todo_params params.require(:todo).permit(:content, :limit,:created_at,:status)#作成日時 #完了ボタン end end
index.html.erb
<p id="notice"><%= notice %></p> <h1><%= link_to 'todoリスト', todos_path %></h1> <h1><%= link_to '検索', todos_search_path %></h1> <%#ホームで入力%> <%= render 'form', todo: @todo %> <table> <thead> <tr> <th>Content</th> <th>Limit</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @todos.each do |todo| %> <tr> <td><%= todo.content %></td> <td><%= todo.limit %></td> <td><%= todo.created_at %></td><%#作成日時%> <td><%= button_to '#{todo.status}', done_path(todo), method: :post %></td><%#完了ボタン%> <td><%= link_to 'Show', todo %></td> <td><%= link_to 'Edit', edit_todo_path(todo) %></td> <td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> </table> <br>
_form.html.erb
<%= form_with(model: todo, local: true) do |form| %> <% if todo.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(todo.errors.count, "error") %> prohibited this todo from being saved:</h2> <ul> <% todo.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= form.label :content %> <%= form.text_area :content, id: :todo_content %> </div> <div class="field"> <%= form.label :limit %> <%= form.date_select :limit, id: :todo_limit %> </div> <div class="actions"> <%= form.submit %> </div> <% end %>
試したこと
rails db:migrateを実行しました
足りない情報があればすぐに付け足します。
よろしくお願いします。
新しいタスクを作った時のログ
Started POST "/todos" for 221.248.118.135 at 2020-03-01 06:15:36 +0000 Cannot render console from 221.248.118.135! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by TodosController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"jZUEysMvyH4I85kgzkFpa+Dc+QID++2CZQA/h9cx9HieMVGJpGSvmX7ewUCib7F0G4OBRvknwtoTe4BOQEX+rA==", "todo"=>{"content"=>"q", "limit(1i)"=>"2020", "limit(2i)"=>"3", "limit(3i)"=>"1"}, "commit"=>"Create Todo"} (0.1ms) begin transaction SQL (0.3ms) INSERT INTO "todos" ("content", "limit", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["content", "q"], ["limit", "2020-03-01"], ["created_at", "2020-03-01 06:15:36.105118"], ["updated_at", "2020-03-01 06:15:36.105118"]] (6.6ms) commit transaction Redirected to https://7afe48de9579467591f9620e4fdfff4a.vfs.cloud9.us-east-2.amazonaws.com/todos.21 Completed 302 Found in 13ms (ActiveRecord: 6.9ms) Started GET "/todos.21" for 221.248.118.135 at 2020-03-01 06:15:36 +0000 Cannot render console from 221.248.118.135! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by TodosController#index as Rendering todos/index.html.erb within layouts/application Rendered todos/_form.html.erb (7.7ms) Todo Load (0.3ms) SELECT "todos".* FROM "todos" ORDER BY "todos"."created_at" DESC Rendered todos/index.html.erb within layouts/application (31.4ms) Completed 200 OK in 50ms (Views: 45.7ms | ActiveRecord: 0.3ms)
回答1件
あなたの回答
tips
プレビュー