Bir makale oluşturmaya ve bir kategori oluşturmaya çalıştığımda, bir blog web uygulaması yaparak öğrenmeye çalışıyorum, raylara sürtünme konusunda yeni biriyim. hata (NoMethodError: nil için tanımlanmamış yöntem "name": NilClass). Kategoriyi dahil etmediğimde ve yalnızca makaleyi oluşturduğumda işler iyi çalışıyor gibi görünüyor. Neyin yanlış olabileceğinden emin değilim, birisi yardım edebilir mi?NoMethodError: undefined method nil: NilClass
ActiveRecord::Schema.define(version: 20160322060108) do
create_table "article_categories", force: true do |t|
t.integer "article_id"
t.integer "category_id"
end
create_table "articles", force: true do |t|
t.string "title", limit: nil
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end
create_table "categories", force: true do |t|
t.string "name"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.datetime "created_at"
end
create_table "users", force: true do |t|
t.string "username", limit: nil
t.string "email", limit: nil
t.string "password_digest", limit: nil
t.boolean "admin", default: false
end
end
kategori modeli:
class Category < ActiveRecord::Base
has_many :article_categories
has_many :articles, through: :article_categories
validates :name, presence: true, length:{ minimum: 3, maximum: 25}
validates_uniqueness_of :name
has_attached_file :avatar, styles: { medium: "270x179>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end
makale modeli:
class Article < ActiveRecord::Base
belongs_to :user
has_many :article_categories
has_many :categories, through: :article_categories
validates :title, presence: true, length:{ minimum: 3, maximum: 60}
validates :description, presence: true, length:{ minimum: 10}
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/missing.jpg"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end
article_category modeli:
class ArticleCategory < ActiveRecord::Base
belongs_to :article
belongs_to :category
end
İşte
Benim db şema olduğunu 0
articles_controller:
class CategoriesController < ApplicationController
before_action :require_admin, except: [:index, :show]
def index
@categories = Category.paginate(page: params[:page], per_page: 9)
end
def new
@category = Category.new
end
def edit
@category = Category.find(params[:id])
end
def update
@category = Category.find(params[:id])
if @category.update(category_params)
flash[:success] = "Category name was successfully updated"
redirect_to category_path(@category)
else
render 'edit'
end
end
def create
@category = Category.new(category_params)
if @category.save
flash[:success] = "Category was created successfully"
redirect_to categories_path
else
render 'new'
end
end
def show
@category = Category.find(params[:id])
@category_articles = @category.articles.paginate(page: params[:page], per_page: 5)
end
private
def category_params
params.require(:category).permit(:name, :avatar)
end
def require_admin
if !logged_in? && (logged_in? and !current_user.admin?)
flash[:danger] = "Only admins can perform that action"
redirect_to categories_path
end
end
end
Hata yaptınız nerede? günlükleri koyabilir misin –
neden 'article_id 'alanını kategori tablonuza ekleyemezsiniz, böylece tablolarınızı ilişkilendirebilirsiniz – Lymuel
sadece terminal resmini ekle aldrien.h –