the correction of a wrong syntax之后我出现了新的错误,我不知道它来自哪里:
在控制台中创建了类别
Category Load (2.0ms) SELECT "categories".* FROM "categories"
=> #<ActiveRecord::Relation [#<Category id: 1, name: "Ruby", created_at: "2016-09-26 09:03:17", updated_at: "2016-09-26 09:03:17">, #<Category id: 2, name: "Rails4", created_at: "2016-09-26 09:03:25", updated_at: "2016-09-27 14:32:39">, #<Category id: 3, name: "Rails5", created_at: "2016-09-26 09:03:30", updated_at: "2016-09-27 14:35:25">, #<Category id: 4, name: "Heroku", created_at: "2016-09-26 09:03:35", updated_at: "2016-09-27 14:35:47">, #<Category id: 5, name: "AWS-Amazon", created_at: "2016-09-26 09:03:43", updated_at: "2016-09-26 09:03:43">]>
当我选择:
malformed format string - %R
malformed format string - %R
malformed format string - %R
malformed format string - %H
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 ...(Maybe have a look on the website filter are not online yet though but you will see the idea)
我应该做这样的事情
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)
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 filter
search
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
进行检查
如果需要,请随意索取更多代码,因为我不确定我错在哪里... 非常感谢 问候
答案 0 :(得分:1)
我修好了!
在我的控制器tutos中我这样做了: 非常感谢芬达以正确的方式带我!
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="btnTeste">
Teste
</button>
答案 1 :(得分:0)
where("name LIKE ?", "%#{filter}%").order('created_at DESC')