質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

819閲覧

フォームオブジェクトモデルに日付情報を渡したい

motoki6318

総合スコア2

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/03/22 02:30

実現したいこと

  • キャンプに行った記録を残すアプリを作る
  • キャンプ記録については日付を入力する
  • キャンプ記録と同時にタグを生成するためフォームオブジェクトモデルを使ってキャンプデータとタグの同時保存をしている
  • フォームオブジェクトモデルで日付データを保存したい

エラーメッセージ

イメージ説明

unknown attribute 'the_day(1i)' for CampTags. Request Parameters: {"authenticity_token"=>"AGNT9e/hOOEKKhzdrb5nAJBwJgVQqMPfZJMsmR/WWHa+czHY6iT2IOeULJSMjCS5IQT/oeeLu9303FrGPwnCVA==", "camp_tags"=>{"title"=>"", "style"=>"", "text"=>"", "the_day(1i)"=>"2015", "the_day(2i)"=>"2", "the_day(3i)"=>"4"}, "commit"=>"Send"}

該当するコード

campsテーブル

class CreateCamps < ActiveRecord::Migration[6.0] def change create_table :camps do |t| t.references :user, null: false, foreign_key: true t.string :title, null: false t.text :text t.date :the_day, null: false t.timestamps end end end

コントローラー

ruby

1class CampsController < ApplicationController 2 before_action :authenticate_user!, except: [:index, :show] 3 before_action :set_item, only: [:index, :new, :show, :create] 4 def index 5 @tags = Tag.all 6 end 7 8 def new 9 @camp = CampTags.new 10 end 11 12 def create 13 @camp = CampTags.new(camp_params) 14 15 if @camp.valid? 16 @tag_list = camp_params[:style].split(/[[:blank:]]+/).select(&:present?) 17 @camp.save(@tag_list) 18 camp = Camp.order(updated_at: :desc).limit(1) 19 @camp_id = camp.ids 20 @item_ids = @camp.item_ids 21 @item_ids.each do |item_id| 22 CampItemRelation.create(camp_id: @camp_id[0], item_id: item_id) 23 end 24 return redirect_to root_path 25 else 26 render "new" 27 end 28 end 29 30 def show 31 @camp = Camp.find(params[:id]) 32 end 33 34 private 35 36 def camp_params 37 params.require(:camp_tags).permit(:title, :style, :text, :the_day, item_ids: []).merge(user_id: current_user.id) 38 end 39 40 def set_item 41 if user_signed_in? 42 user = User.find(current_user.id) 43 @items = user.items 44 end 45 end 46 47end

フォームオブジェクトモデル

class CampTags include ActiveModel::Model attr_accessor :title, :style, :text, :the_day, :user_id, :item_ids with_options presence: true do validates :user_id validates :title validates :the_day validates :style validates :item_ids end def save(tag_list) @camp = Camp.create(user_id: user_id, title: title, text: text, the_day: the_day) tag_list.each do |tag| unless Tag.find_by(style: tag) @tag = Tag.create(style: tag) CampTagRelation.create(camp_id: @camp.id, tag_id: @tag.id) else @tag_id = Tag.find_by(style: tag) CampTagRelation.create(camp_id: @camp.id, tag_id: @tag_id.id) end end end end

入力フォーム

<div class="wrapper"> <div class="side-ber"> <%= render "side_ber" %> </div> <%= form_with model: @camp, url: camps_path, class:'form-wrap', local: true do |f| %> <%= render 'layouts/error_messages', model: f.object %> <div class='form'> <div class="camp-field"> <%= f.label :title, "キャンプタイトル" %> <%= f.text_field :title, class:"input-title" %> </div> <div class="camp-field"> <%= f.label :style, "キャンプスタイル" %> <%= f.text_field :style, class:"input-tag" %> </div> <div class="camp-text-field"> <%= f.label :style, "キャンプの感想" %> <%= f.text_area :text, class:"input-text" %> </div> <div class="camp-day-field"> キャンプ日付<br> <%= raw sprintf( f.date_select( :the_day, class:'select-birth', use_month_numbers: true, prompt:'--', start_year: (Time.now.year - 10), date_separator: '%s'), "年", "月") + "日" %> </div> <p>使用アイテムを選択 </p> <select name="camp_tags[item_ids][]" multiple> <% @items.each do |item| %> <option value=<%= item.id %>><%= item.name %></option> <% end %> </select> </div> <div class="submit-post"> <%= f.submit "Send", class: "submit-btn" %> </div> <% end %> </div>

試したこと

paramを確認すると日付データが"the_day(1i)"=>"2015", "the_day(2i)"=>"2", "the_day(3i)"=>"4"となっていることから日付の保存の記述を変えてみた。↓↓↓

def save(tag_list) @camp = Camp.create(user_id: user_id, title: title, text: text, the_day: the_day(1i), the_day: the_day(2i), the_day: the_day(3i)) tag_list.each do |tag| unless Tag.find_by(style: tag) @tag = Tag.create(style: tag) CampTagRelation.create(camp_id: @camp.id, tag_id: @tag.id) else @tag_id = Tag.find_by(style: tag) CampTagRelation.create(camp_id: @camp.id, tag_id: @tag_id.id) end end end

@camp = Camp.create(user_id: user_id, title: title, text: text, the_day: the_day(1i), the_day: the_day(2i), the_day: the_day(3i))の部分です。
この対処は文法が間違っているらしくだめでした。

どうかご教授お願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

入力フォームを<%= f.date_field :day %>にすることで解決しました。

投稿2021/03/22 05:32

motoki6318

総合スコア2

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問