Reputation: 1566
Thought I understood custom routes, but apparently not since it's not working.
Currently have this set up in Forecast
model:
class Forecast < ApplicationRecord
def get_weather_data_paris
ForecastIO.forecast(48.8566, 2.3522)
end
end
It works for ForecastsController
show action ..., i.e., this:
def show
@weather = @forecast.get_weather_data_paris
@current_weather = @weather.currently
@daily_weather = @weather.daily.data.first(5)
end
Decided that I wanted to set up a separate page for different locations, so, copied the stuff from def show
method to def show_paris
method:
def show_paris
@weather = @forecast.get_weather_data_paris
@current_weather = @weather.currently
@daily_weather = @weather.daily.data.first(5)
end
Added the action to the routes:
Rails.application.routes.draw do
resources :forecasts
root to: 'paris', to: "forecasts#show_paris"
end
Now, when I visit the home page, I get this error:
NoMethodError in ForecastsController#show_paris`
undefined method 'get_weather_data_paris' for nil:NilClass`
Why is this happening? What am I missing?
If it helps, here is the entire 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
Upvotes: 1
Views: 65
Reputation: 33471
Try defining the content of your forecasts#show_paris
method as:
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
And then in your routes.rb
:
root to: 'paris', to: 'forecasts#show_paris', id: 1
This way you're passing an id
in order that your @forecast
variable isn't nil
.
Upvotes: 1