Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 NoMethodError:未定义的方法`name';零级:零级_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 NoMethodError:未定义的方法`name';零级:零级

Ruby on rails 4 NoMethodError:未定义的方法`name';零级:零级,ruby-on-rails-4,Ruby On Rails 4,我是RubyonRails新手,尝试通过博客web应用程序学习,当我试图创建一篇文章并在其中包含一个类别时,我会遇到错误(NoMethodError:nil:NilClass的未定义方法'name')。 当我不包括类别,只是创建文章时,事情似乎很顺利。我不确定可能出了什么问题,有人能帮忙吗 以下是我的数据库模式: ActiveRecord::Schema.define(version: 20160322060108) do create_table "article_categories",

我是RubyonRails新手,尝试通过博客web应用程序学习,当我试图创建一篇文章并在其中包含一个类别时,我会遇到错误(NoMethodError:nil:NilClass的未定义方法'name')。 当我不包括类别,只是创建文章时,事情似乎很顺利。我不确定可能出了什么问题,有人能帮忙吗

以下是我的数据库模式:

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
类别模型:

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
class ArticleCategory < ActiveRecord::Base
  belongs_to :article
  belongs_to :category
end
类别”,thumb:“100x100>”},默认url:“/images/:style/missing.png”
验证附件内容类型:头像,内容类型:/\Aimage\/.\Z/
结束
文章范本:

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
类文章”,thumb:“100x100>”},默认url:“/images/missing.jpg”
验证附件内容类型:头像,内容类型:/\Aimage\/.\Z/
结束
第11条类别模式:

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
class ArticleCategory < ActiveRecord::Base
  belongs_to :article
  belongs_to :category
end
class ArticleCategory
财务主任:

class ArticlesController < ApplicationController

  before_action :set_article, only: [:edit, :update, :show, :destroy]
  before_action :require_user, except: [:index, :show]
  before_action :require_same_user, only: [:edit, :update, :destroy]

  def index
    @articles = Article.paginate(page: params[:page], per_page: 5)
  end

  def new
    @article = Article.new
  end

  def edit
  end

  def create
    @article = Article.new(article_params)
    @article.user = current_user
    if @article.save
      flash[:success] = "Article was successfully created"
      redirect_to article_path(@article)
    else
      render 'new'
    end
  end

  def update
    if @article.update(article_params)
      flash[:success] = "Article was successfully updated"
      redirect_to article_path(@article)
    else
      render 'edit'
    end
  end

  def show
  end

  def destroy
    @article.destroy
    flash[:danger] = "Article was successfully deleted"
    redirect_to articles_path
  end

  private
    def set_article
      @article = Article.find(params[:id])
    end

    def article_params
      params.require(:article).permit(:title, :description, :avatar, category_ids: [])
    end

    def require_same_user
      if current_user != @article.user and !current_user.admin?
        flash[:danger] = "You can only edit or delete your own articles"
        redirect_to root_path
      end
    end

end
class-ArticlesController
和控制器

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
class CategoriesController

此错误是由过滤器引起的。在所有控制器中删除之前的操作并尝试

问题是Ruby版本,将版本更改为Ruby 2.1,现在一切正常。

您从哪里得到错误?你能把日志放进去吗?为什么不把
article\u id
字段添加到你的分类表中,这样你就可以关联你的表呢?刚刚添加了终端图片aldrien.hc你能在你尝试创建
@article
时提供你的
分类参数吗?它在分类控制器的底部,在private部分下