自分の職歴を投稿する機能を作成しています。
job_category(職種)、annual_salary(年収)、position(役職)はデータベースに反映されるのですが、
start_date(仕事開始日) と end_date(仕事終了日) がデータベースに反映されずに躓いております。
アドバイスをいただけますと幸いです。
views/careers/new.html.erb
<div> <%= form_with model: @career, local: true do |f| %> <%= f.text_field :job_category %> <%= f.text_field :annual_salary %> <%= f.text_field :position %> <%= f.label :start_date %> <%= f.date_select( :start_date, start_year: 1930, end_year: (Time.now.year - 18), default: Date.new(1989, 1, 1)) %> <%= f.label :end_date %> <%= f.date_select( :end_date, start_year: 1930, end_year: (Time.now.year - 18), default: Date.new(1989, 1, 1)) %> <%= f.submit "送信", class: "btn btn-primary withripple" %> <% end %> </div>
controllers/careers_controller.rb
def new @career = Career.new end def create Career.create( job_category: career_params[:job_category], annual_salary: career_params[:annual_salary], position: career_params[:position], start_date: career_params[:start_date], end_date: career_params[:end_date], user_id: current_user.id ) end def career_params params.require(:career).permit(:job_category, :annual_salary, :position, :start_date, :end_date) end
エラー
Started POST "/careers" for 127.0.0.1 at 2018-12-09 14:07:37 +0900 Processing by CareersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXXXXXXXXXXXX/X/XXXXXX==", "career"=>{"job_category"=>"100", "annual_salary"=>"200", "position"=>"300", "start_date(1i)"=>"1989", "start_date(2i)"=>"1", "start_date(3i)"=>"4", "end_date(1i)"=>"1990", "end_date(2i)"=>"2", "end_date(3i)"=>"3"}, "commit"=>"送信"} User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 9 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/controllers/careers_controller.rb:72 (0.4ms) BEGIN ↳ app/controllers/careers_controller.rb:17 User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 9 LIMIT 1 ↳ app/controllers/careers_controller.rb:17 Career Create (4.5ms) INSERT INTO `careers` (`job_category`, `created_at`, `updated_at`, `user_id`, `annual_salary`, `position`) VALUES ('100', '2018-12-09 14:07:37', '2018-12-09 14:07:37', 9, 200, '300') ↳ app/controllers/careers_controller.rb:17 (81.8ms) COMMIT ↳ app/controllers/careers_controller.rb:17 #<ActiveModel::Errors:0x00007f8901294e38 @base=#<Career id: 64, job_category: "100", created_at: "2018-12-09 05:07:37", updated_at: "2018-12-09 05:07:37", user_id: 9, annual_salary: 200, position: "300", start_date: nil, end_date: nil>, @messages={}, @details={}> Rendering careers/create.html.erb within layouts/application Rendered careers/create.html.erb within layouts/application (0.7ms) Completed 200 OK in 722ms (Views: 613.2ms | ActiveRecord: 88.1ms)
"start_date(1i)"=>"1989", "start_date(2i)"=>"1", "start_date(3i)"=>"4", "end_date(1i)"=>"1990", "end_date(2i)"=>"2", "end_date(3i)"=>"3"
という(1i)〜(3i)がうまく情報渡せていないように見えます。
追記です。
controllerのcreateにて career_params を読み込むように変えてみましたが、
今度は ROLLBACK が起きて全て登録できなくなってしまいました…。
controllers/careers_controller.rb
def new @career = Career.new end def create @career = Career.create(career_params) logger.debug @career.errors.inspect end
エラー:
Started POST "/careers" for 127.0.0.1 at 2018-12-09 22:40:58 +0900 Processing by CareersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"Q3E41N8ZMgNky4zx6TH/thUk2rVqQMmJpDprCBhbuUi+AjQINyLj6rz9h1hZxjzejUyrQ4xBtRBYbmOIOqLMkg==", "career"=>{"job_category"=>"111", "annual_salary"=>"2222", "position"=>"3333", "start_date(1i)"=>"1989", "start_date(2i)"=>"1", "start_date(3i)"=>"1", "end_date(1i)"=>"1989", "end_date(2i)"=>"1", "end_date(3i)"=>"1"}, "commit"=>"送信"} User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 9 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/controllers/careers_controller.rb:86 (0.3ms) BEGIN ↳ app/controllers/careers_controller.rb:36 (2.0ms) ROLLBACK ↳ app/controllers/careers_controller.rb:36 #<ActiveModel::Errors:0x00007fd55dc2fc88 @base=#<Career id: nil, job_category: "111", created_at: nil, updated_at: nil, user_id: nil, annual_salary: 2222, position: "3333", start_date: "1989-01-01", end_date: "1989-01-01">, @messages={:user=>["を入力してください"]}, @details={:user=>[{:error=>:blank}]}> Rendering careers/create.html.erb within layouts/application Rendered careers/create.html.erb within layouts/application (0.5ms) Completed 200 OK in 278ms (Views: 225.5ms | ActiveRecord: 3.4ms)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。