如何在“link_to”中传递隐藏参数
有没有办法通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试
动态注入 javascript,将链接转换为表单,并通过发布
希望有帮助的方式提交参数 =)
you can try
this will dynamically inject javascripts to turn the links into a form and submit the parameters through posting
hope that helps =)