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

发布于 2024-08-22 08:56:03 字数 1293 浏览 12 评论 0原文

有没有办法通过 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,但两者都不起作用。有没有办法做到这一点,或者我只是完全菜鸟?

Is there a way to pass parameters with a link_to call without it showing up on the URL? I'm making a simple star-rating system, and I'm basically making each star an image link that passes its value as a parameter to a new rendering of the same page. The helper code looks like this:

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 

It works perfectly, but currently the star rating shows up as a param in the URL. I would ideally want to hide that information somehow, and I've tried both hidden_field_tag and hidden_tag, neither of which work. Is there no way to do this or am i just completely noob?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2024-08-29 08:56:03

您可以尝试

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

动态注入 javascript,将链接转换为表单,并通过发布

希望有帮助的方式提交参数 =)

you can try

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

this will dynamically inject javascripts to turn the links into a form and submit the parameters through posting

hope that helps =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文