Comment résoudre cette erreur:erreur de routage méthode non définie < » pour nil: NilClass dans des rails
[! [Enter image description ici] [1]] [1]
Je suis en train de mettre en méthode de validation personnalisés dans le modèle, mais je reçois
Routing Error undefined method `<' for nil:NilClass
Ce code est le modèle de l'article
# article.rb
class Article < ActiveRecord::Base
validates_presence_of :category_id, :title, :body, :is_publish, :publish_date, :feature_image_url
validates_numericality_of :category_id, greater_than: 0
validates_length_of :title, minimum: 5
validates_length_of :body, within: 5..200
validates_uniqueness_of :title
validate :publish_date_cannot_be_more_than_one_month_from_today
def publish_date_cannot_be_more_than_one_month_from_today
if publish_date > (Date.today + 1.month)
errors.add(:publish_date, "Can't be more than 1 month from today")
end
end
end
This is code for articles_controller.rb articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to articles_path
else
render action: "new"
end
end
def show
@article = Article.find(params[:id])
@category = Category.find(@article.category_id)
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update_attributes(article_params)
redirect_to articles_path
else
render action: "new"
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
a = Article.new
a.publish_date_cannot_be_more_than_one_month_from_today
private
def article_params
params[:article].permit(:title, :body, :is_publish, :publish_date, :category_id, :feature_image_url)
end
end
Ajoutez votre modèle de l'article à la question, et utiliser le code (texte) au lieu des images. –
Pourriez-vous ajouter des extraits de code? Spécifiquement article modèle et article contrôleur –
comment résoudre ce problème? Quelqu'un peut-il me dire –