Rails - 未初始化的常量ArticlesController ::文章

时间:2015-06-24 03:19:03

标签: ruby-on-rails ruby controller

请帮助,不知道我做错了什么。我只想尝试制作一个简单的搜索表单,而且我猜测我的路线中可能有错误,但我在调试方面并不是很强,因为我相当新

错误是 -

  

未初始化的常量ArticlesController ::文章

routes.rb -

<!DOCTYPE html>
<head>
  <title>Simple Search Form</title>
</head>
<body>
  <!-- When submit the form, the view rendered will be the index view of our articles resource -->
  <%= form_tag(articles_path, :method => "get", class: "navbar-form", id: "search-form") do %>
    <div class="input-append">
      <%= text_field_tag :search, params[:search], class: "span2", placeholder: "Search Articles" %>
      <!-- In order to have the "search" icon int the button, we need to use plain HTML instead 
           of using a Rails form helper -->
      <button class="btn" type="submit"><i class="icon-search"></i></button>
    </div>
  <% end %>
  <%= yield %>
</body>

articles.rb -

def Article < ActiveRecord::Base
  attr_accessible :title, :content

  validates :title, presence: true, uniqueness: true
  validates :content, presence: true

  # It returns the articles whose titles contain one or more words that form the query
  def self.search(query)
    # where(:title, query) -> This would return an exact match of the query
    where("title like ?", "%#{query}%") 
  end
end

articles_controller.rb ***这就是错误在哪里****它突出显示&#34; @articles = Article.order(&#34; create_at DESC&#34;)&#34;

class ArticlesController < ApplicationController
  def index
    if params[:search]
      @articles = Article.search(params[:search]).order("created_at DESC")
    else
      **@articles = Article.order("created_at DESC")**
    end
  end
end

index.html.erb -

    <% @articles.each do |article| %>
  <div class="article">
    <h1 class="article-title"><%= link_to article.title, article %></h1>
    <p class="article-content"><%= article.content %></p>
  </div>
<% end %>

application.html.erb -

<!DOCTYPE html>
<head>
  <title>Simple Search Form</title>
</head>
<body>
  <!-- When submit the form, the view rendered will be the index view of our articles resource -->
  <%= form_tag(articles_path, :method => "get", class: "navbar-form", id: "search-form") do %>
    <div class="input-append">
      <%= text_field_tag :search, params[:search], class: "span2", placeholder: "Search Articles" %>
      <!-- In order to have the "search" icon int the button, we need to use plain HTML instead 
           of using a Rails form helper -->
      <button class="btn" type="submit"><i class="icon-search"></i></button>
    </div>
  <% end %>
  <%= yield %>
</body>

2 个答案:

答案 0 :(得分:2)

在articles.rb中你有

def Article < ActiveRecord::Base

你应该

class Article < ActiveRecord::Base

然后你应该好好去。

答案 1 :(得分:1)

您的代码需要实例化一个新类。现在你正在创建一个以某种方式从ActiveRecord继承的方法。

这(在article.rb中):

def Article < ActiveRecord::Base
end

应该是:

class Article < ActiveRecord::Base
end
相关问题