创建新文章时遇到问题

时间:2019-03-11 13:27:01

标签: ruby-on-rails

我正在尝试在Ruby on Rails博客中实现类别,但是在尝试创建新文章时收到一些错误。 在ArticlesController#new

中找不到没有ID的ActiveRecord:RecordNotFound的文章

这是我的Article控制器代码:

class ArticlesController < ApplicationController
#http_basic_authenticate_with name: "orko", password: "1234567",
#except: [:index, :show, :search]

before_action :edit, :admin_authorize, :except => [:index, :show, :search]

  def index
    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

商品模型:

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

  #For photo special characters \A, \w, \z
  has_attached_file :photo, styles: {large: "450x450>", thumb: "50x50#"}
  validates_attachment_content_type :photo, content_type: /\Aimage\/.*\z/

  #For music upload
  has_attached_file :music
  validates_attachment :music,
  :content_type => {:content_type => ["audio/mpeg","audio/mp3"]},
  :file_type => {:matches => [/mp3\Z/]}

  #For movie
  has_attached_file :movie, :styles =>
  {
    :medium => {:geometry => "640x480", :format=> 'mp4'},
    :thumb => {:geometry => "100x50", :format => 'jpg', :time => 10}
  }, :processor => [:transcoder]
  validates_attachment_content_type :movie, content_type: /\Avideo\/.*\z/

  def self.search(params)
    articles = Article.where("text LIKE ? or title LIKE ?", "%#{params[:search]}%",
                                                            "%#{params[:search]}%") if params[:search].present?
    articles # returns the articles containing the search words
  end
end

和类别模型:

class Category < ApplicationRecord
  has_many :articles
end

这是我的路线文件:

Rails.application.routes.draw do
#  get 'sessions/new'

  resources :sessions
  resources :users
  get 'welcome/index'

  resources :articles do
    resources :comments
    collection do
      get :search #creates a new path for searching
    end
  end
  resources :subscribers
  root 'welcome#index'
  get 'pages/about' => 'application#show'
end

2 个答案:

答案 0 :(得分:1)

在此参数中,您添加了:search方法。请删除搜索方法并进行检查。

   > params.require(:article).permit(:title, :text, :search, :music,
    > :movie, :photo)

只需尝试在Rails控制台中创建文章即可。

答案 1 :(得分:0)

您正在edit上调用before_action方法,尝试将其删除或将newcreate添加到except数组中。