railsでToDoアプリの作成をしているのですが
date型の日付と外部キーのカテゴリーがDBに登録されません。
他のフォームは登録されるのですが、この2つだけ未入力と判断されてしまします。
プログラミング初心者でどうすればいいかわかりません。
TasksController
1class TasksController < ApplicationController 2 before_action :set_task, only: [:show, :edit, :update, :destroy] 3 before_action :login_check, only: [:new, :edit, :update, :destroy, :index, :comp] 4 5 # GET /tasks 6 # GET /tasks.json 7 def index 8 @tasks = current_user.tasks.all 9 10 end 11 12 def comp 13 @tasks = current_user.tasks.all 14 end 15 16 17 # GET /tasks/new 18 def new 19 @task = Task.new 20 end 21 22 # POST /tasks 23 # POST /tasks.json 24def create 25 id = params[:id] 26 27 @task = Task.new(params[:task]) 28 @task.user = current_user 29 30 if @task.save 31 redirect_to '/tasks' , notice: 'タスクを作成しました' 32 else 33 render 'new' 34 end 35 end 36 37 private 38 def task_params 39 params.require(:task).permit( 40 :title, :status, :due_date, :category_id_id) 41 end 42
HTML
1<table calss="attr"> 2<%= form_for(@task) do |form| %> 3<tr> 4 <th><%= form.label :title, "タイトル" %></th> 5 <td><%= form.text_field :title, id: :title %></td> 6</tr> 7<tr> 8 <th><%= form.label :category_id_id, "カテゴリー" %></th> 9 <td><%= form.collection_select :category_id_id, Category.all, :id, :category%></td> 10</tr> 11<tr> 12 <th><%= form.label :status, "状況" %></th> 13 <td><%= form.select :status, Task.statuses.keys,:selected=>Task.statuses[@task.status] %><td> 14</tr> 15<tr> 16 <th><%= form.label :due_date, "期限" %></th> 17 <td><%= form.date_field :due_date %></td> 18</tr> 19<%= form.hidden_field :user_id, value: current_user.id %> 20 21 22</table> 23<%= form.submit "登録"%><br> 24<%= link_to '戻る', tasks_path %> 25 26<% end %>
tasksMigration
1class CreateTasks < ActiveRecord::Migration[5.2] 2 def change 3 create_table :tasks do |t| 4 t.references :category_id, foreign_key: true 5 t.references :user, foreign_key: true 6 t.string :title 7 t.date :due_date 8 t.integer :status, default: 0 9 10 11 end 12 end 13end
categoriesMigration
1class CreateCategories < ActiveRecord::Migration[5.2] 2 def change 3 create_table :categories do |t| 4 t.string :category 5 t.timestamps 6 end 7 end 8end
taskmodel
1class Task < ApplicationRecord 2 belongs_to :category, required: false 3 belongs_to :user 4 5 enum status:[:未完了, :完了] 6 7 validates :title, presence: true 8 validates :category_id_id, presence: true 9 validates :status, presence: true 10 validates :due_date, presence: true 11end 12
categorymodel
1class Category < ApplicationRecord 2 has_many :tasks 3end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/11 06:30
2020/01/11 06:33
2020/01/11 08:49 編集
2020/01/11 14:50
2020/01/11 21:20
2020/01/12 15:51