rails4 – 格式错误的格式字符串

纠正错误的语法后,我有一个新的错误,我不知道它来自哪里:

类别是在控制台中创建的

Category Load (2.0ms) SELECT "categories".* FROM "categories" => #<ActiveRecord::Relation [#, #, #, #, #]> 

当我选择:

  • “Ruby”我有: malformed format string - %R
  • “Rails4”我有: malformed format string - %R
  • “Rails5”我有: malformed format string - %R
  • 我有“Heroku”: malformed format string - %H
  • “AWS-Amazon”我有: too few arguments

我想按名称过滤我的类别:

类别模型

 class Category < ActiveRecord::Base has_many :tutos def self.filter(filter) if filter where(["name LIKE '%#{filter}%'"]).order('created_at DESC') else all end end end 

第一编辑

我尝试过不同的方式,但每个方面都有问题:

 `where(["name LIKE '%#{filter}%'"]).order('created_at DESC')` 

返回格式错误的字符串警报

 `where("name LIKE '%#{filter}%'").order(created_at: :desc)` 

 `where("name LIKE ?", "%#{filter}%").order('created_at DESC')` 

返回所有类别而不过滤….

你可能想看到tutos / views / index

 .container .row h1.text-gray Tutorials .row.search_banner .col-xs-3 =form_tag tutos_path, :method => 'get' do =text_field_tag :search, params[:search], placeholder:"Search by keywords" =submit_tag "Search", class:'btn btn-xs btn-default btn-search' .col-xs-3 =form_tag tutos_path, :method => 'get' do =select_tag :filter, options_for_select(Category.all.map{|c| c.name}, params[:filter]) =submit_tag "Search", class:"btn btn-xs btn-default btn-search" .col-xs-3 -if user_signed_in? = link_to "Create a tuto", new_tuto_path, class:"btn btn-success" br br #tutos.transitions-enabled -@tutos.each do |tuto| .box.panel-default = link_to image_tag(image_by_category(tuto.category.try(:name))), tuto_path(tuto) h3 = link_to tuto.title, tuto_path(tuto), class:"title-link" h6 | Created by: span = tuto.user.full_name br span.glyphicon.glyphicon-heart span = tuto.get_upvotes.size br br 

第二次编辑

Rails控制台日志:如果我搜索“Heroku”Cartegory:但由于我的类别属于tutos …( 可能看一下网站filter还没有在线,但你会看到这个想法 )

我应该这样做吗?
Tuto.category.where("name LIKE ?", "%#{filter}%").order('created_at DESC')

 Started GET "/tutos?utf8=%E2%9C%93&filter=Heroku&commit=Search" for ::1 at 2016-09-28 17:50:41 +0200 Processing by TutosController#index as HTML Parameters: {"utf8"=>"✓", "filter"=>"Heroku", "commit"=>"Search"} Category Load (0.3ms) SELECT "categories".* FROM "categories" User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] Tuto Load (0.1ms) SELECT "tutos".* FROM "tutos" User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (1) Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (2, 3, 5, 4, 1) (0.1ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 16], ["votable_type", "Tuto"], ["vote_flag", "t"]] (0.1ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 17], ["votable_type", "Tuto"], ["vote_flag", "t"]] (0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 18], ["votable_type", "Tuto"], ["vote_flag", "t"]] (0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 19], ["votable_type", "Tuto"], ["vote_flag", "t"]] (0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 20], ["votable_type", "Tuto"], ["vote_flag", "t"]] (0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 21], ["votable_type", "Tuto"], ["vote_flag", "t"]] (0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 22], ["votable_type", "Tuto"], ["vote_flag", "t"]] (0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 23], ["votable_type", "Tuto"], ["vote_flag", "t"]] (0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 24], ["votable_type", "Tuto"], ["vote_flag", "t"]] Rendered tutos/index.html.slim within layouts/application (29.0ms) Rendered layouts/_nav.html.slim (0.4ms) Completed 200 OK in 104ms (Views: 94.1ms | ActiveRecord: 1.9ms) 

第3编辑

tuto模型

 class Tuto < ActiveRecord::Base acts_as_votable belongs_to :user belongs_to :category validates :category_id, presence: true def self.search(search) if search where(["title LIKE ?","%#{search}%"]).order('created_at DESC') else all end end end 

类别模型我像tuto search一样构建我的类别filter

 class Category < ActiveRecord::Base has_many :tutos def self.filter(filter) if filter #where(["name LIKE '%#{filter}%'"]).order('created_at DESC') #where("name LIKE '%#{filter}%'").order(created_at: :desc) where("name LIKE ?", "%#{filter}%").order('created_at DESC') else all end end end 

检查建议的raise 'check' 我有这个错误:

如果需要,请随意索取更多代码,因为我不确定我错在哪里…非常感谢

我修好了它 !

在我的控制器tutos中我做到了这一点:非常感谢芬达以正确的方式带我!

 def index #binding.pry if params[:search].present? @tutos = Tuto.search(params[:search]).includes(:user, :category) else @tutos = Tuto.all.includes(:user, :category) end if params[:filter].present? @tutos = Tuto.joins(:category).where('categories.name LIKE ?', params[:filter]) else @categories = Category.all end end 
 where("name LIKE ?", "%#{filter}%").order('created_at DESC')