前提・実現したいこと
gemのcocoonを使って1つのフォームから複数のモデルに保存したい。
現在、投稿機能を持ったアプリケーションを開発しています。
ひとつのフォームから複数のモデルに保存する。
動的にフォームを追加・削除をしたい。
この2つからgemのcocoonを使用することにしましたが、、うまく機能せず困っています。
お力をお借りしたいです。
初めて質問をするので、至らないことが多いと思いますが、よろしくお願いします。
実現したいこと
page のnew.html.erbのフォームから
pageのpage_titleとpage_commentを
spotのspot_name,spot_address,spot_tel,spot_parking(radio_button),spot_commentを保存し、
spotの部分は動的にフォームの追加・削除ができるようにしたいです。
発生している問題・エラーメッセージ
エラーメッセージ NoMethodError in Pages#new Showing projects/tyorippu2/app/views/pages/_page_spot_fields.html.erb where line #3 raised: undefined method `text_field' for #<Spot:0x00007f8642f516b0>
該当のソースコード
ルーティング
Ruby
1app/config/routes.rb 2 3Rails.application.routes.draw do 4 devise_for :users 5 root to: 'pages#index' 6 resources :pages do 7 resources :spots 8 end 9end 10
モデル
Ruby
1app/models/page.rb 2 3class Page < ApplicationRecord 4 belongs_to :user 5 has_many :spots, dependent: :destroy, inverse_of: :page 6 accepts_nested_attributes_for :spots, allow_destroy: true, reject_if: :all_blank 7 8 validates :page_title, presence: true 9end
Ruby
1app/models/spot.rb 2 3class Spot < ApplicationRecord 4 belongs_to :page 5 6 validates :spot_name, presence: true 7 validates :spot_address, presence: true 8end
コントローラー
Ruby
1app/controllers/pages_controller.rb 2 3class PagesController < ApplicationController 4 5 def index 6 @pages = Page.all 7 end 8 9 def new 10 @page = Page.new 11 @spot = @page.spots.build 12 end 13 14 def create 15 @page = Page.new(page_params) 16 if @page.save 17 redirect_to root_path 18 else 19 render :new 20 end 21 end 22 23 24 private 25 26 def page_params 27 params.require(:page).permit(:pege_title, :page_comment, 28 spot_attributes:[:id, :spot_name, :spot_address, :spot_tel, :spot_parking, :spot_comment, :_destroy, :page_id]).merge(user_id: current_user.id) 29 end 30end
ビュー
Ruby
1app/views/pages/new.html.erb 2 3<%= render "shared/second-header" %> 4 5<%= form_with(model: @page, local: true ) do |f| %> 6 <%# pageテーブル保存したい %> 7 <div class="field"> 8 <%= f.text_field :page_title, class:"input-default", id:"page-name", placeholder:"ページタイトルは?", maxlength:"40" %> 9 </div> 10 <div class="field"> 11 <%= f.text_field :page_comment, class:"input-default", id:"page-comment", placeholder:"どんなコースかひとことでどうぞ!", maxlength:"40" %> 12 </div> 13<%# ページのメイン部分 %> 14 <div class='spot-form'> 15 <h1 class='info-input-headline'> 16 コース 17 </h1> 18 19 <%# spotテーブル %> 20 <div id="spots"> 21 <%= f.fields_for :spots do |spot| %> 22 <%= render 'pages/page_spot_fields', f: spot %> 23 24 <div class="addNewSpot" id="addNewSpot"> 25 <%= link_to_add_association "add spot", f, :spots, 26 class:"btn btn-default", 27 data: { 28 association_insertion_node: '#detail-association-insertion-point', 29 association_insertion_method: 'after' 30 } 31 %> 32 </div> 33 <% end %> 34 </div> 35 <%# spotテーブル %> 36 37 38 39 <%# 送信ボタン %> 40 <%= f.submit "作成する" %> 41<% end %> 42 <%# 送信ボタン %>
Ruby
1app/views/pages/_page_spot_fields.html.erb 2 3<div class="nested-fields"> 4 <div class="field"> 5 ⇩この部分がエラーになってしまいます。 6 <%= @spot.text_field :spot_name, class:"input-default", id:"spot-name", placeholder:"場所の名前" %> 7 </div> 8 <div class="field"> 9 <%= @spot.text_area :spot_address, class:"input-default", id:"spot-address", placeholder:"住所(都道府県は絶対に入力してください)" %> 10 </div> 11 <div class="field"> 12 <%= @spot.text_field :spot_tel, class:"input-default", id:"spot-tel", placeholder:"電話番号" %> 13 </div> 14 <div class="field"> 15 <%= @spot.label :spot_parking, "駐車場:" %> 16 <%= @spot.radio_button :spot_parking, "あり" %>あり 17 <%= @spot.radio_button :spot_parking, "なし" %>なし 18 <%= @spot.radio_button :spot_parking, "近隣に有料あり" %>近隣に有料あり 19 </div> 20 <div class="field"> 21 <%= @spot.text_field :spot_comment, class:"input-default", id:"spot-comment", placeholder:"あなたの感想をひとことどうぞ!" %> 22 </div> 23 <%= link_to_remove_association "remove spot", s %> 24</div>
試したこと
いろんなサイトなどを見て、f.fields_forを@page.spotsなどに変更してみたりと色々試しました。
また、renderの部分を確認しましたが、わかりませんでした。
補足情報(FW/ツールのバージョンなど)
rails ver6.0.0
gem 'devise'
gem 'cocoon'
gem 'jquery-rails'
json
1package.json 2 3{ 4 "name": "tyorippu2", 5 "private": true, 6 "dependencies": { 7 "@nathanvda/cocoon": "^1.2.14", 8 "@rails/actioncable": "^6.0.0-alpha", 9 "@rails/activestorage": "^6.0.0-alpha", 10 "@rails/ujs": "^6.0.0-alpha", 11 "@rails/webpacker": "4.3.0", 12 "jquery": "^3.6.0", 13 "turbolinks": "^5.2.0" 14 }, 15 "version": "0.1.0", 16 "devDependencies": { 17 "webpack-dev-server": "^4.1.0" 18 } 19}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/22 14:03