我有一个包含带有文本框的表单的视图。文本框的值应反映在网址中。
在文本框中输入并提交值时,网址和文本框会反映构建视图时的值。
当在网址中更改了值时,我却无法让文本框反映该值。
当我调试视图时,我通过表单提交@filter[:destination]
包含我期望的值,当我通过url(/ holiday / spain)提交时,@ filter [:destination]给出nil。
发生了什么事?
观点是:
<%= form_tag '/holiday/all', id: 'search_form', method: :get do %>
<div id="location" class="collapse in">
<div class="padding20">
<%= text_field :filter, :destination, value: @filter[:destination], class: 'form-control' %>
<div class="clearfix"></div>
<br/>
<%= button_tag class: 'btn-search4', id: 'search_button' do %>
<i class="fa fa-search"></i> Search
<% end %>
</div>
</div>
routes.rb的相关部分:
get 'holiday/:location', to: 'holidays#index', as: 'holidays'
最后是控制器:
page = params[:page] || 1
sort = params[:sort] || {}
@filter = params[:filter] || {}
display_type = params[:display_type] || {}
# set hidden field values
@filter[:duration_min] = @filter[:duration_min] || 1
@filter[:duration_max] = @filter[:duration_max] || 31
@filter[:destination] = params[:location]
@result = Holiday.search type: display_type, page: page, sort: sort, filter: @filter
通过网址提交后记录:
Started GET "/vakantie/Kreta" for ::1 at 2016-04-20 22:41:18 +0200
Processing by HolidaysController#index as HTML
Parameters: {"location"=>"Kreta"}
(17.4ms) SELECT COUNT(*) FROM "holidays" WHERE ((upper(city) like upper('%Kreta%') OR upper(region) like upper('%Kreta%') or upper(country) like upper('%Kreta%')) AND (duration_min >= 1 AND duration_max <= 31 ))
Holiday Load (3.2ms) SELECT "holidays".* FROM "holidays" WHERE ((upper(city) like upper('%Kreta%') OR upper(region) like upper('%Kreta%') or upper(country) like upper('%Kreta%')) AND (duration_min >= 1 AND duration_max <= 31 )) ORDER BY id asc LIMIT 12 OFFSET 0
CACHE (0.3ms) SELECT COUNT(*) FROM "holidays" WHERE ((upper(city) like upper('%Kreta%') OR upper(region) like upper('%Kreta%') or upper(country) like upper('%Kreta%')) AND (duration_min >= 1 AND duration_max <= 31 ))
Rendered partials/_starrating.html.erb (3.7ms)
Review Load (1.1ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."holiday_id" = $1 LIMIT 1 [["holiday_id", 804]]
Rendered partials/_userrating.html.erb (12.0ms)
Rendered partials/_detail.html.erb (139.4ms)
Rendered partials/_starrating.html.erb (3.8ms)
Review Load (1.0ms) SELECT "reviews".* FROM "reviews" WHERE Completed 200 OK in 1467ms (Views: 1415.5ms | ActiveRecord: 40.5ms)
通过表单提交后记录:
Started GET "/vakantie/Spanje?utf8=%E2%9C%93&filter%5Bduration_min%5D=1&filter%5Bduration_max%5D=31&filter%5Bprice_min%5D=1&filter%5Bprice_max%5D=500&filter%5Bdestination%5D=Spanje&button=&filter%5Bdays%5D=1%3B31&filter%5Bprice%5D=1%3B500&filter%5Bis_hotel%5D=false&filter%5Bis_appartement%5D=false&filter%5Bis_aparthotel%5D=false&filter%5Bis_villa%5D=false&filter%5Bis_bungalow%5D=false&filter%5Bis_holidaypark%5D=false&filter%5Bis_camping%5D=false&filter%5Bbus%5D=false&filter%5Bflight%5D=false&filter%5Bself_transportation%5D=false&filter%5Bhas_wifi%5D=false&filter%5Bhas_kids_pool%5D=false&filter%5Bhas_pool%5D=false&filter%5Bhas_supermarket%5D=false&filter%5Bhas_bar%5D=false&filter%5Bhas_parking%5D=false&filter%5Bhas_restaurant%5D=false&filter%5Bservice_logies%5D=false&filter%5Bservice_breakfast%5D=false&filter%5Bservice_halfpension%5D=false&filter%5Bservice_allinclusive%5D=false&filter%5Bstars_five%5D=false&filter%5Bstars_four%5D=false&filter%5Bstars_three%5D=false&filter%5Bstars_two%5D=false&filter%5Bstars_one%5D=false&sort%5Bname%5D=Naam&sort%5Bprice%5D=Prijs" for ::1 at 2016-04-20 22:49:21 +0200
Processing by HolidaysController#index as HTML
Parameters: {"utf8"=>"✓", "filter"=>{"duration_min"=>"1", "duration_max"=>"31", "price_min"=>"1", "price_max"=>"500", "destination"=>"Spanje", "days"=>"1;31", "price"=>"1;500", "is_hotel"=>"false", "is_appartement"=>"false", "is_aparthotel"=>"false", "is_villa"=>"false", "is_bungalow"=>"false", "is_holidaypark"=>"false", "is_camping"=>"false", "bus"=>"false", "flight"=>"false", "self_transportation"=>"false", "has_wifi"=>"false", "has_kids_pool"=>"false", "has_pool"=>"false", "has_supermarket"=>"false", "has_bar"=>"false", "has_parking"=>"false", "has_restaurant"=>"false", "service_logies"=>"false", "service_breakfast"=>"false", "service_halfpension"=>"false", "service_allinclusive"=>"false", "stars_five"=>"false", "stars_four"=>"false", "stars_three"=>"false", "stars_two"=>"false", "stars_one"=>"false"}, "button"=>"", "sort"=>{"name"=>"Naam", "price"=>"Prijs"}, "location"=>"Spanje"}
(18.6ms) SELECT COUNT(*) FROM "holidays" WHERE ((upper(city) like upper('%Spanje%') OR upper(region) like upper('%Spanje%') or upper(country) like upper('%Spanje%')) AND (duration_min >= 1 AND duration_max <= 31 AND price_max >= 1 AND price_min <= 500 ))
Holiday Load (1.6ms) SELECT "holidays".* FROM "holidays" WHERE ((upper(city) like upper('%Spanje%') OR upper(region) like upper('%Spanje%') or upper(country) like upper('%Spanje%')) AND (duration_min >= 1 AND duration_max <= 31 AND price_max >= 1 AND price_min <= 500 )) LIMIT 12 OFFSET 0
答案 0 :(得分:0)
在表单
中使用text_field_tag
代替text_field
答案 1 :(得分:0)
如果是符号/字符串问题,您可以使用控制器中的Hash#with_indifferent_access
将Hash
转换为HashWithIndifferentAccess
。
因此匹配字符串和键指的是相同的值而不是不同的值