前提・実現したいこと
staffs/index.html.erbでは同じようにform_forを記述して問題なく動作するのに、stattic_pages/home.html.erbで同じようにform_forを記述するとエラーがでマス。
質問は
・form_forは数あるコントローラーの中からなにを持ってどのコントローラーにいけば良いのか判断するのか?
・このエラーが出ているform_forはTimetableControllerに飛んでいないということですよね?
・なぜ、今回のようなエラーが出てしまうのか?
です!!
発生している問題・エラーメッセージ
form_forを使ってデータベースの内容を更新したいのですが、下記のエラーが出てうまくいきません。なにが原因なのでしょうか?
ArgumentError in StaticPages#home Showing /home/ec2-user/environment/portfolio/app/views/helpers/_time_table.html.erb where line #120 raised: First argument in form cannot contain nil or be empty Extracted source (around line #120): 118 119 120 121 122 123 </div> <%= form_for(@timetable, :url => {:controller => :timetables,:action => :new}) do |f| %> <%= f.text_field :name %> <%= f.submit %> <% end %> Trace of template inclusion: app/views/static_pages/home.html.erb Rails.root: /home/ec2-user/environment/portfolio Application Trace | Framework Trace | Full Trace app/views/helpers/_time_table.html.erb:120:in `_app_views_helpers__time_table_html_erb__2879188542577283771_28453940' app/views/static_pages/home.html.erb:27:in `_app_views_static_pages_home_html_erb__207691314488552288_28208260' Request Parameters: None Toggle session dump Toggle env dump Response Headers: None
該当のソースコード
ruby
1※問題のページ 2 3 4stattic_pages/home.html.erb 5 6<%= form_for(@timetable) do |f| %> 7 <%= f.text_field :reservation1 %> 8 <%= f.submit %> 9<% end %>
ruby
1class TimetablesController < ApplicationController 2 def timetable_index 3 @timetables = Timetable.all 4 end 5 6 def new 7 @timetable = Timetable.new 8 end 9 10 def create 11 end 12 13 def destroy 14 end 15 16end 17
ruby
1※このコードはちゃんと動作します! 2timetables/timetable_index.html.erb 3 4 5<div class="home_table"> 6<table class="table table-bordered table-dark"> 7 <thead> 8 <tr> 9 <td colspan="5"><h4>本日の予約状況(スタッフ専用)</h4></td> 10 </tr> 11 </thead> 12 <tbody> 13 <% @timetables.each do |a| %> 14 <tr> 15 <td><%= a.time %></td> 16 <td><%= a.reservation1 %></td> 17 <td><%= a.reservation2 %></td> 18 <td><%= a.reservation3 %></td> 19 <td><%= a.reservation4 %></td> 20 </tr> 21 <% end %> 22</table> 23</div> 24
ruby
1 root 'static_pages#home' # => root_path 2 get '/contact', to: 'static_pages#contact' 3 get '/campaign', to: 'static_pages#campaign' 4 get '/signup', to: 'users#new' 5 get '/timetable', to: 'users#timetable' 6 post '/signup', to: 'users#create' 7 8 get '/login', to: 'sessions#new' 9 post '/login', to: 'sessions#create' 10 delete '/logout', to: 'sessions#destroy' 11 12 get '/staffsignup', to: 'staffs#new' 13 post '/staffsignup', to: 'staffs#create' 14 15 get '/stafflogin', to: 'sessions_for_staff#new' 16 post '/stafflogin', to: 'sessions_for_staff#create' 17 delete '/stafflogout', to: 'sessions_for_staff#destroy' 18 19 get '/stafftimetable', to: 'timetables#timetable_index' 20 21 resources :users 22 resources :staffs 23 resources :account_activations, only: [:edit] 24 resources :microposts, only: [:create, :destroy] 25 resources :timetables 26 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 27end 28
ruby
1 2seeds.rb 3 4 5[ 6 ['9:00~9:30', '', '', '', ''], 7 ['9:30~10:00', '', '', '', ''], 8 ['10:00~10:30', '', '', '', ''], 9 ['10:30~11:00', '', '', '', ''], 10 ['11:00~11:30', '', '', '', ''], 11 ['11:30~12:00', '', '', '', ''], 12 ['15:00~15:30', '', '', '', ''], 13 ['15:30~16:00', '', '', '', ''], 14 ['16:00~16:30', '', '', '', ''], 15 ['16:30~17:00', '', '', '', ''], 16 ['17:00~17:30', '', '', '', ''], 17 ['17:30~18:00', '', '', '', ''], 18 ['18:00~18:30', '', '', '', ''], 19 ['18:30~19:00', '', '', '', ''], 20 ['19:00~19:30', '', '', '', ''], 21 ['19:30~20:00', '', '', '', ''], 22 ['20:00~20:30', '', '', '', ''] 23].each do |time, nothing1, nothing2, nothing3, nothing4| 24 Timetable.create!( 25 { time: time, reservation1: nothing1, 26 reservation2: nothing2, 27 reservation3: nothing3, 28 reservation4: nothing4 } 29 ) 30 end 31コード
ruby
1class StaticPagesController < ApplicationController 2 def home 3 if logged_in_as_staff? 4 @micropost = current_staff.microposts.build 5 @feed_items = current_staff.feed.paginate(page: params[:page]) 6 end 7 end 8 9 def contact 10 end 11 12 def campaign 13 end 14 15 def new 16 @timetable = Timetable 17 end 18end 19
補足情報(FW/ツールのバージョンなど)
Rails Tutorial と同じ環境でやりました。
回答2件
あなたの回答
tips
プレビュー