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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1375閲覧

createとupdateで値が入らない&一覧表示ができない

Reo23

総合スコア15

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2017/07/23 09:44

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

ご教授いただけると幸いです。
よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

createやupdateの時にバリデーションエラー等で更新に失敗する場合には、下記コードによりindexのテンプレートをレンダリングしようとしています。

format.html { render action: 'index' }

この時、index.html.erbでは@call_histsを参照していますが、createやupdateメソッドでは@call_histsに値を代入していないためnilとなっていてエラーになっています。

解決するためには、createとupdateのformat.html { render action: 'index' }の前にて、@call_histsにCallHist.allなど表示したいCallHistの一覧を設定しておけば良いかと思います。

投稿2017/08/12 07:04

yuutetu

総合スコア88

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

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

Reo23

2017/08/18 06:46

うまくいきました。誠にありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問