如何在“link_to”中传递隐藏参数

时间:2010-02-17 08:37:16

标签: ruby-on-rails hidden-field link-to rating-system

有没有办法通过link_to调用传递参数而不显示在URL上?我正在制作一个简单的星级评级系统,我基本上将每个星形成为一个图像链接,将其值作为参数传递给同一页面的新渲染。帮助程序代码如下所示:

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中的参数。理想情况下,我想以某种方式隐藏这些信息,我已经尝试过hidden_​​field_tag和hidden_​​tag,两者都不起作用。有没有办法做到这一点,还是我只是完全没有人?

1 个答案:

答案 0 :(得分:0)

你可以尝试

link_to image_tag('star_rated.png', :mouseover => 'star_hover.png'), review_new_url(:stars => star), :method => :post

这将动态注入javascript以将链接转换为表单并通过发布提交参数

希望有帮助=)