2016-05-22 220 views
0

這是我的搜索形式:控制器不讀SEARCH_INPUT,Ruby on Rails的

<%= form_tag("/kalender/:search_input", method: "get") do %> 
     <%= label_tag(:search_input, "Suche:") %> 
     <%= text_field_tag(:search_input) %> 
     <%= submit_tag("search") %> 
    <% end %> 

這是我的控制器,得到了SEARCH_INPUT:

@tag = params[:search_input] 
if @tag.nil? || @tag.empty? 
    @events = Event.all 
else 
    @events = Event.tagged_with("#{@tag}") 
end 

這是我的路線:

get "/kalender/:search_input" => 'kalender#kalender' 

問題似乎是,該行:Event.tagged_with("#{@tag}")不起作用。

當我在導軌控制檯中運行時,此命令:Event.tagged_with("Metaphysik") 2已找到條目,它的工作方式應該如此。

任何想法?提前致謝!

編輯:在軌控制檯:

irb(main):002:0> Event.tagged_with("Metaphysik") 
    ActsAsTaggableOn::Tag Load (0.4ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('metaphysik')) 
    Event Load (0.5ms) SELECT "events".* FROM "events" JOIN taggings events_taggings_65a1b36 ON events_taggings_65a1b36.taggable_id = "events".id AND events_taggings_65a1b36.taggable_type = 'Event' AND events_taggings_65a1b36.tag_id = 3 
=> #<ActiveRecord::Relation [#<Event id: 75, title: "Loigk", description: "p", user_id: 1, created_at: "2016-05-22 15:05:05", updated_at: "2016-05-22 15:05:05">, #<Event id: 76, title: "Metaphysik", description: "d", user_id: 1, created_at: "2016-05-22 15:52:30", updated_at: "2016-05-22 15:52:30">]> 
irb(main):003:0> 

服務器,在Metaphysiker鍵入時:

Started GET "/kalender/:search_input?utf8=%E2%9C%93&search_input=Metaphysiker&commit=search" for 127.0.0.1 at 2016-05-22 20:49:58 +0200 
Processing by KalenderController#kalender as HTML 
    Parameters: {"utf8"=>"✓", "search_input"=>":search_input", "commit"=>"search"} 
    ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER(':search_input')) 
    Event Load (0.3ms) SELECT "events".* FROM "events" WHERE (1 = 0) 
    Rendered kalender/_fullkalendar.html.erb (3.2ms) 
    Rendered kalender/kalender.html.erb within layouts/application (6.0ms) 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 
Completed 200 OK in 294ms (Views: 169.1ms | ActiveRecord: 1.2ms) 
+0

控制器運行'Event.tagged_with(「#{@ tag}」)時產生的SQL是什麼? – Uzbekjon

+0

@Uzbekjon我發佈了SQL生成的 – Metaphysiker

+0

你的控制器很好 - 問題在於你的路由 –

回答

1

你的形式結束了以下措施:action="/kalender/:search_input"。從字面上看,它有:search_input作爲URL。但你期待像kalender/your_input_string

路線更改爲:

get "/kalender/search" => 'kalender#kalender' 

而在你KalenderController#search行動,params[:search_input]訪問搜索字符串。