为什么我不能创建任何类别的文章?

时间:2019-03-17 18:02:38

标签: ruby-on-rails ruby

我是RoR方面的新手,并且正在开发Blog应用程序,并实现文章的类别。但是我遇到了麻烦-当我创建具有某些类别(“运动”或“电影”或其他类别)的任何文章时,都会收到验证错误

 - Category must exist
 - Category can't be blank

但是我有可用的下拉列表或类别(此助手):

  def categories
    category =
      ["Sport",
      "Movie",
      "Art",
      "Nature",
      "Exotic"]
    category.each do |categ|
      my_category = "#{categ}"
    end
    return category
  end

这是我的article.new.html.erb文件的一部分代码:

  <p>
    <%= f.label :category %><br>
    <%= f.select :category, categories,
        prompt: "Choose your category" %>
  </p>

这也是我的数据库模式,其中存在类别字段:

  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "pic"
    t.string "photo_file_name"
    t.string "photo_content_type"
    t.bigint "photo_file_size"
    t.datetime "photo_updated_at"
    t.string "music_file_name"
    t.string "music_content_type"
    t.bigint "music_file_size"
    t.datetime "music_updated_at"
    t.string "movie_file_name"
    t.string "movie_content_type"
    t.bigint "movie_file_size"
    t.datetime "movie_updated_at"
    t.string "category_id"
  end

  create_table "categories", force: :cascade do |t|
    t.string "name"
    t.text "desc"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "comments", force: :cascade do |t|
    t.string "commenter"
    t.text "body"
    t.bigint "article_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["article_id"], name: "index_comments_on_article_id"
  end

  create_table "subscribers", force: :cascade do |t|
    t.string "f_name"
    t.string "l_name"
    t.string "email"
    t.string "country"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "userid"
    t.string "email"
    t.string "password_digest"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "admin", default: false
  end

  add_foreign_key "comments", "articles"
end

这是我的模特:

class Article < ApplicationRecord
  belongs_to :category
  has_many :comments, dependent: :destroy
  validates :title, presence: true, length: {minimum: 3}
  validates :text, presence: true, length: {minimum: 3}
  validates :category_id, presence: true
end

class Category < ApplicationRecord
  has_many :articles
end

这也是我的Article控制器:

class ArticlesController < ApplicationController
before_action :admin_authorize, :except => [:index, :show, :search]

  def index
    @articles = Article.includes(:category).order("created_at DESC")
    if params[:category].blank?
      @articles = Article.all.order("created_at DESC")
    else
      @category_id = Category.find_by(name: params[:category]).id
      @articles = Article.where(category_id: @category_id).order("created_at DESC")
    end
  end

  def new
    @article = Article.new
    @categories = Category.all.map{|c| [c.name, c.id]}
  end

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

  def create
    @article = Article.new(article_params)
    @article.category_id = params[:category_id]

    respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: "Article was successfully created!" }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new}
        format.json {render json: @article.errors, status: :unprocessable_entity}
      end
    end
  end

  def edit
    @article = Article.find(params[:id])
    @categories = Category.all.map{|c| [ c.name, c.id ] }
  end

  def search
    if params[:search].blank?
      @articles = Article.all
    else
      @articles = Article.search(params)
    end
  end

  def update
    @article = Article.find(params[:id])
    @article.category_id = params[:category_id]
    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to articles_path
  end

private
  def article_params
    params.require(:article).permit(:title, :text, :search, :music, :movie, :photo)
  end

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

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您在允许的参数中忘记了category_id

def article_params
  params.require(:article).permit(:title, :text, :search, :music, :movie, :photo, :category_id)
end

您还需要更改选择助手,以便在POST请求中发送category_id而不发送category
现在,借助您的categories助手,您无需在select下拉列表中发送任何类别ID,仅发送一些未绑定到任何Category实例的“类别”名称。

您可以像这样修复select并删除您的助手:

<p>
  <%= f.label :category %><br>
  <%= f.select :category_id, Category.all.collect{|c| [c.name, c.id]},
    prompt: "Choose your category" %>
</p>