3
有沒有辦法通過link_to調用傳遞參數而不顯示在URL上?我正在製作一個簡單的星級評分系統,我基本上是讓每個星星成爲一個圖像鏈接,將它的價值作爲參數傳遞給同一頁面的新呈現。助手代碼如下所示:如何在「link_to」中傳遞隱藏的參數
def stars_generator(edit_mode = false)
@rating = params[:stars].to_i #takes rating from page param, so :star must be defined first!
@stars = Array.new(5) {|i| i+1} #change array size for more stars
output = "<div class = 'star_container'>"
case edit_mode #checks whether to display static stars or clickable stars
when true
@stars.each do |star| #this block generates empty or colored stars depending the value of @rating and the position of the star evaluated
if star <= @rating
output += link_to image_tag('star_rated.png', :mouseover => 'star_hover.png'), review_new_url(:stars => star)
else
output += link_to image_tag('star_empty.png', :mouseover => 'star_hover.png'), review_new_url(:stars => star)
end
end
when false #static stars are displayed if edit_mode is false
@stars.each do |star|
if star <= @rating
output += image_tag('star_rated.png')
else
output += image_tag('star_empty.png')
end
end
end
output += "</div>"
return output
end
它完美,但目前的星級顯示爲在URL中設置了一個param。理想情況下,我想以某種方式隱藏這些信息,並且我已經嘗試了hidden_field_tag和hidden_tag,但這兩者都不起作用。有沒有辦法做到這一點,或者我只是完全noob?
嗯提交的參數,添加:方法=>:帖子沒有爲我做任何事情,但我沒有在項目中的任何JavaScript。有沒有辦法做到這一點沒有js? – funkymunky 2010-02-19 07:09:25
將數據發送到Web服務器而不顯示URL的唯一方法是發佈表單。如果您沒有任何JavaScript框架供rails使用,那麼'method::post'將不起作用,您將不得不構建標準表單,並在提交時發佈隱藏字段。您可以將圖像作爲表單按鈕 – maniacalrobot 2015-12-14 20:16:42