2017-09-23 1 views
-1

서버 상태가 위 또는 아래인지 확인하기 위해 루비 앱을 만들고 싶습니다. 나중에 json 경로를 수정하여 다양한 구문으로 길을 찾을 수 있습니다.정의되지 않은 메소드`each 'for nil : NilClass - 왜?

핑 컨트롤러 :

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 

서비스 컨트롤러 :하지만 지금, 나는 더러운 "NilClass 정의되지 않은 메서드`각 '무기 호에 대한"와 붙어있어 (여기 내 설치는 대한

있습니다

<%= @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 %> 
: 서비스 나는)

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 

보기 (서비스 지수)를 추가 할

+2

어디'services' 정의되는 : 당신이보기를 렌더링하는 데 사용하는 컨트롤러, 당신은 두 인스턴스 변수를 정의해야으로 시도한다 무엇 이건

? –

+0

@sebastianpalma 서비스 컨트롤러를 추가합니다. – Doge

+0

질문에 추가 한 뷰와 어떤 컨트롤러가 일치합니까? –

답변

2

@services 반복 내부에있는 @pings을 반복하려고합니다. 그러나 이것은 정의되어 있지 않으며 서비스 만 정의했으며 each이있는 객체가있는 경우에는 ping의 반복이 작동하지 않습니다. 적용된 값은 nil입니다.

def index 
    @services = Service.all 
    @pings = Ping.all 
end 
+0

그냥 해냈어, 고마워, 고마워. – Doge

관련 문제