http://qiita.com/jacoyutorius/items/ea0673fe30cfe7cdac8f#%E6%9B%B4%E6%96%B0%E7%B7%A8
この記事を参考にindexページで作っていますがうまくいきません。
現在ののコードで実行すると、
NoMethodError in CallHists#createまたはNoMethodError in CallHists#update
undefined method `each' for nil:NilClass
Extracted source (around line #75): 75 <% @call_hists.each do |call_hist| %> 76 <p><%= call_hist.created_at %></p> 77 <p><%= call_hist.cmp_name %></p> 78 <p><%= call_hist.contact %></p>
となってしまいます。
Ruby
1call_hists_controller.rb 2 3class CallHistsController < ApplicationController 4before_action :set_call_hist, only: [:show, :edit, :update, :destroy] 5 6 7 def create 8 @call_hist = CallHist.new(call_hist_params) 9 @call_hist.save 10 respond_to do |format| 11 if @call_hist.save 12 format.html { redirect_to call_hists_path, notice: 'Item was successfully created.' } 13 format.json { render action: 'index', status: :created, location: @call_hist } 14 else 15 format.html { render action: 'index' } 16 format.json { render json: @call_hist.errors, status: :unprocessable_entity } 17 end 18 end 19 end 20 21 def index 22 @call_hists = CallHist.all.order(created_at: :desc) 23 if params[:id].present? 24 set_call_hist 25 else 26 @call_hist = CallHist.new 27 end 28 end 29 30 def update 31 respond_to do |format| 32 if @call_hist.update(call_hist_params) 33 format.html { redirect_to request.referer, notice: 'Item was successfully updated.' } 34 format.json { head :no_content } 35 else 36 format.html { render action: 'index' } 37 format.json { render json: @call_hist.errors, status: :unprocessable_entity } 38 end 39 end 40 end 41 42 def destroy 43 set_call_hist 44 @call_hist.destroy 45 respond_to do |format| 46 format.html { redirect_to "/call_hists" } 47 format.json { head :no_content } 48 end 49 end 50 51 private 52 53 def set_call_hist 54 @call_hist = CallHist.find_by(id: params[:id]) 55 end 56 57 def call_hist_params 58 params.require(:call_hist).permit( :date, :time, :tel, 59 :operator, 60 :cmp_name, :call_id, :tanyu_id, 61 :fig1, :fig2, :fig3, :fig4, :fig5, 62 :csi_no, :memo, :csi_data, 63 :tanyu_ahd, 64 :cosmo_id, :contact) 65 end 66 67end 68
HTML
1index.html.erb 2 3 4<%= link_to "マイページ",call_hists_path %> 5<div> 6 <input type="button" value="履歴作成・編集" class="write_call_hist"> 7 <input type="button" value="閉じる" class="close_form"> 8</div> 9 10<div> 11 <%= session[:operator_id] %> 12 <%= link_to("ログアウト", "/logout", {method: :post}) %> 13</div> 14 15<div class="form"> 16 <%= form_for (@call_hist) do |f| %> 17 18 <div class="field"> 19 <%= f.label :cmp_name, "ユーザー" %> 20 <%= f.text_field :cmp_name %> 21 </div> 22 23 <div class="field"> 24 <%= f.label :contact, "連絡者" %> 25 <%= f.text_field :contact %> 26 </div> 27 28 <div class="field"> 29 <%= f.label :tel, "電話番号" %> 30 <%= f.text_field :tel %> 31 </div> 32 33 <div class="field"> 34 <%= f.label :csi_no, "CSI NO." %> 35 <%= f.text_field :csi_no %> 36 </div> 37 38 <div class="field"> 39 <%= f.label :fig4, "受電方法" %> 40 <%= f.collection_select :fig4, CallHist.all, :id, :fig4 %> 41 </div> 42 43 <div class="field"> 44 <%= f.label :fig5, "転送先" %> 45 <%= f.collection_select :fig5, CallHist.all, :id, :fig5 %> 46 </div> 47 48 <div class="field"> 49 <%= f.label :memo %> 50 <%= f.text_area :memo %> 51 </div> 52 53 <div class="field"> 54 <%= f.label :tanyu_id, "リモート依頼" %> <%= f.check_box :tanyu_id %> 55 <%= f.label :tanyu_ahd, "依頼先" %> 56 <%= f.collection_select :tanyu_ahd, CallHist.all, :id, :tanyu_ahd %> 57 </div> 58 59 <div class="field"> 60 <%= f.label :csi_data %> 61 <%= f.text_area :csi_data %> 62 </div> 63 64 <div class="field"> 65 <%= f.label :fig1, "1." %> <%= f.check_box :fig1 %> 66 <%= f.label :fig1, "2." %> <%= f.check_box :fig2 %> 67 <%= f.label :fig1, "3." %> <%= f.check_box :fig3 %> 68 </div> 69 70 <div class="btn"> 71 <%= f.submit %> 72 </div> 73 74 <% end %> 75</div> 76 77 78<% @call_hists.each do |call_hist| %> 79 <p><%= call_hist.created_at %></p> 80 <p><%= call_hist.cmp_name %></p> 81 <p><%= call_hist.contact %></p> 82 <p><%= call_hist.tel %></p> 83 <p><%= link_to 'Edit', call_hists_path(id: call_hist.id) %></p> 84 <p><%= link_to 'Destroy', call_hist_path(id: call_hist.id), method: :delete, data: { confirm: 'Are you sure?' } %></p> 85<% end %> 86
ご教授いただけると幸いです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/18 06:46