2017-03-23 10 views
1

Dachte ich verstand benutzerdefinierte Routen, aber anscheinend nicht, da es nicht funktioniert.Benutzerdefinierte Route funktioniert nicht

Zeit eingestellt haben dies in Forecast Modells:

class Forecast < ApplicationRecord 

    def get_weather_data_paris 
    ForecastIO.forecast(48.8566, 2.3522) 
    end 

end 

Es funktioniert für ForecastsController show-Aktion ..., das heißt das:

def show  
    @weather = @forecast.get_weather_data_paris  
    @current_weather = @weather.currently 
    @daily_weather = @weather.daily.data.first(5) 
end 

Entschieden, dass ich eine separate einrichten wollte Seite für verschiedene Standorte, so kopierte das Zeug von def show Methode zu def show_paris Methode:

def show_paris 
    @weather = @forecast.get_weather_data_paris  
    @current_weather = @weather.currently 
    @daily_weather = @weather.daily.data.first(5) 
end 

Hinzugefügt wurde die Aktion auf den Strecken:

Rails.application.routes.draw do 
    resources :forecasts 
    root to: 'paris', to: "forecasts#show_paris" 
end 

Nun, wenn ich die Homepage besuchen, bekomme ich diesen Fehler:

NoMethodError in ForecastsController#show_paris` 
undefined method 'get_weather_data_paris' for nil:NilClass` 

Warum ist das passiert? Was vermisse ich?

Wenn es hilft, ist hier die gesamte ForecastsController

class ForecastsController < ApplicationController 
    before_action :set_forecast, only: [:show, :edit, :update, :destroy] 

    # GET /forecasts 
    # GET /forecasts.json 
    def index 
    @forecasts = Forecast.all 
    end 

    def show  
    @weather = @forecast.get_weather_data_paris  
    @current_weather = @weather.currently 
    @daily_weather = @weather.daily.data.first(5) 
    end 

    def show_paris 
    @weather = @forecast.get_weather_data_paris  
    @current_weather = @weather.currently 
    @daily_weather = @weather.daily.data.first(5) 
    end 

    # GET /forecasts/new 
    def new 
    @forecast = Forecast.new 
    end 

    # GET /forecasts/1/edit 
    def edit 
    end 

    # POST /forecasts 
    # POST /forecasts.json 
    def create 
    @forecast = Forecast.new(forecast_params) 

    respond_to do |format| 
     if @forecast.save 
     format.html { redirect_to @forecast, notice: 'Forecast was successfully created.' } 
     format.json { render :show, status: :created, location: @forecast } 
     else 
     format.html { render :new } 
     format.json { render json: @forecast.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /forecasts/1 
    # PATCH/PUT /forecasts/1.json 
    def update 
    respond_to do |format| 
     if @forecast.update(forecast_params) 
     format.html { redirect_to @forecast, notice: 'Forecast was successfully updated.' } 
     format.json { render :show, status: :ok, location: @forecast } 
     else 
     format.html { render :edit } 
     format.json { render json: @forecast.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /forecasts/1 
    # DELETE /forecasts/1.json 
    def destroy 
    @forecast.destroy 
    respond_to do |format| 
     format.html { redirect_to forecasts_url, notice: 'Forecast was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_forecast 
     @forecast = Forecast.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def forecast_params 
     params.require(:forecast).permit(:lat, :lng) 
    end 
end 

Antwort

1

Versuchen Sie den Inhalt Ihrer forecasts#show_paris Methode definieren als:

def show_paris 
    @forecast = Forecast.find(params[:id]) 

    @weather = @forecast.get_weather_data_paris  
    @current_weather = @weather.currently 
    @daily_weather = @weather.daily.data.first(5) 
end 

Und dann in Ihrem routes.rb:

root to: 'paris', to: 'forecasts#show_paris', id: 1 

Auf diese Weise passierst du eine id, damit Ihre @forecast Variable nicht nil ist.

+0

danke für deine antwort! Es hat mich verrückt gemacht! Würden Sie zufällig etwas über Low-Level Caching von Rails wissen? – user273072545345

+0

Ich würde empfehlen, Standardparameter in der Steuerung zu setzen: 'params [: id]? Forecast.find (params [: id]): Forecast.first' – Ilya

+0

@llya, meinst du das so: '@forecast = params [: id]? Forecast.find (params [: id]): Forecast.first' – user273072545345

Verwandte Themen