RoR:我如何只搜索具有特定属性的微博?
问题描述:
眼下在app /视图/微柱/ home.html.erb我..RoR:我如何只搜索具有特定属性的微博?
<% form_tag purchases_path, :method => 'get', :id => "products_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<% form_tag sales_path, :method => 'get', :id => "sales_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
,然后在micropost.rb我有
scope :purchases, where(:kind => "purchase")
scope :sales, where(:kind => "sale")
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
,然后终于在microposts_controller.rb我有
def home
@microposts=Micropost.all
@[email protected]
@[email protected]
end
现在我得到一个错误说未定义的局部变量或方法`purchases_path”,它确实为sales_path相同。
我想要做的只是搜索一些微博,而不是全部微博。在我的微博表中,我有一个名为kind的专栏,可以是“购买”或“销售”。如何更改这三段代码,以便一次搜索搜索并仅显示那些类型为“购买”的微博的结果。然后其他的搜索通过,并在结果中显示只有在一种“买卖”
这个问题(在另一篇文章)的微柱具有RoR: how can I search only microposts with a specific attribute?
答
50代表一个赏金你可以试试这个。
你的模型:
class Micropost
# ...
scope :purchase_only, where(:kind => "purchase")
# ...
def self.search(search)
if search
self.purchase_only.find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
self.purchase_only
end
end
end
但是,这东西看起来很奇怪,我。
例如: - 你应该删除.find(...)
,该探测器将使用Rails弃用4.
+0
你是对的。我按照rails 3更新了我的问题,但是你的答案没有工作:(它给出了一个路由错误,没有方法GET/[microposts]。你能看看我的编辑,看看我做错了什么吗? – BigBoy1337
为什么要使用旧的语法,而不是'where'条款? – apneadiving
我正在学习本教程http://railscasts.com/episodes/37-simple-search-form。也许这是我的问题 – BigBoy1337