以为我理解自定义路线,但显然不是因为它不起作用。
目前已在Forecast
型号中设置此项:
class Forecast < ApplicationRecord
def get_weather_data_paris
ForecastIO.forecast(48.8566, 2.3522)
end
end
适用于ForecastsController
show动作......,即:
def show
@weather = @forecast.get_weather_data_paris
@current_weather = @weather.currently
@daily_weather = @weather.daily.data.first(5)
end
决定我想为不同的地点设置单独的页面,因此,将这些内容从def show
方法复制到def show_paris
方法:
def show_paris
@weather = @forecast.get_weather_data_paris
@current_weather = @weather.currently
@daily_weather = @weather.daily.data.first(5)
end
在路线中添加了操作:
Rails.application.routes.draw do
resources :forecasts
root to: 'paris', to: "forecasts#show_paris"
end
现在,当我访问主页时,我收到此错误:
NoMethodError in ForecastsController#show_paris`
undefined method 'get_weather_data_paris' for nil:NilClass`
为什么会这样?我错过了什么?
如果有帮助,这是整个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
答案 0 :(得分:1)
尝试将forecasts#show_paris
方法的内容定义为:
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
然后在routes.rb
:
root to: 'paris', to: 'forecasts#show_paris', id: 1
这样您就会传递id
,以使@forecast
变量不是nil
。