2017-09-23 19 views
-1

Rubyアプリケーションを作成して、サーバーのステータスがアップまたはダウンしているかどうかを確認します。 後で、私のjsonパスに適応して多様な構文の方法を見つける方法を見つけるでしょう。しかし、今のところ、私は汚いとこだわっている "nilのための未定義のメソッド`各」:NilClass"未定義のメソッド `each 'for nil:NilClass - なぜですか?

のPingコントローラ:

def index 
    @pings = Ping.all 
    end 

    def new 
    @service = Ping.new 
    end 

    def create 
    @ping = Ping.new(ping_params) 
    @ping.service = @service 
    @ping.up = test_ping 
    @ping.save 
    end 

    def test_ping 
    require 'json' 
    require 'open-uri' 

    url = 'https://www.facebook.com/platform/api-status/' 
    fb_status_serialized = open(url).read 
    fb_status = JSON.parse(fb_status_serialized) 

    if fb_status['current']['health'] == 1 
     test_ping = true 
    else 
     test_ping = false 
    end 
    end 

    private 

    def ping_params 
    params.require(:ping).permit(:service_id, :up) 
    end 

    def set_ping 
    @ping = Ping.find(params[:ping_id]) 
    end 
end 

サービスコントローラ:

(ここでは、のための私の設定です私は追加したいサービス)

class ServiceController < ApplicationController 

    before_action :set_service, only: [:edit, :show, :destroy] 

    def index 
    @services = Service.all 
    end 

    def new 
    @service = Service.new 
    end 

    def edit 
    end 

    def show 
    end 

    def create 
    @service = Service.new(service_params) 
    @service.save 
     if @service.save 
    redirect_to service_path(@service) 
    else 
     render :new 
    end 
    end 

    def destroy 
    @service.destroy 
    @service.destroy 
    redirect_to services_path 
    end 

private 
    def set_service 
     @service = Service.find(params[:id]) 
    end 
    def service_params 
     params.require(:service).permit(:name, :web_api, :json_path) 
    end 

end 

ビュー(サービスインデックス):

<%= @services.each do | s | %> 
    <p><%= s.name %></p> 
    <p><%= s.web_api %></p> 
    <p><%= s.json_path %></p> 
    <p><%= s.id %></p> 

    <%= @pings.each do | p | %> 
    <%# if p.service_id == s.id %> 
    <!-- undefined --> 
    <% end %> 

    <p>|||</p> <%= Ping.new(up: true, service: s) %> <p>|||</p> 
<% end %> 
+2

? –

+0

@sebastianpalmaサービスコントローラーを追加します – Doge

+0

質問に追加したビューにどのコントローラーが対応していますか? –

答えて

2

@services繰り返し内にある@pingsを繰り返し処理しようとしていますが、これは定義されておらず、サービスのみを定義しており、eachのオブジェクトが存在する場合はpingの繰り返しは機能しません適用される値はnilです。

、あなたは両方のインスタンス変数を定義するビューをレンダリングするために使用しているコントローラ必要が何であれ、と試してみてください。

の `services`が定義されている
def index 
    @services = Service.all 
    @pings = Ping.all 
end 
+0

ちょうど、それは働く、ありがとう – Doge

関連する問題