如何使用选择菜单和link_to重定向到另一个页面-Ruby on Rails [英] How to redirect to another page using a select menu and link_to - Ruby on Rails

查看:80
本文介绍了如何使用选择菜单和link_to重定向到另一个页面-Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用选择菜单重定向到另一个页面,但是当我选择主页"时,它不会将我重定向到主页.什么都没发生...

I want to use a select menu to redirect to another page but when I select 'Home' it doesn't redirect me to the main page. Nothing happens...

<select>
    <option>Select a menu</option>
    <option><%= link_to 'Home', '/' %></option>
</select>

谢谢您的帮助.

推荐答案

link_to帮助器输出一个HTML超链接,该超链接嵌套在option元素中时将不起作用.您应该更改选项,使它们具有以下格式:

The link_to helper outputs an HTML hyperlink which is not going to work when nested within an option element. You should change your options so that they have this format:

<option value="/">Home</option>

然后,您可以使用一些JavaScript来观察下拉菜单的onchange事件,并将document.location.href设置为所选选项的值.这将执行重定向.

Then you could have some JavaScript that observes the onchange event of the dropdown and sets the document.location.href to the value of the selected option. This will perform the redirect.

或者,您可以在下拉菜单旁边有一个 Go 提交按钮,用于提交表单,然后让Rails使用redirect_to帮助器将服务器端重定向到所选选项的页面. .

Alternatively, you could have a Go submit button next to the dropdown that submits the form and then have Rails perform a server-side redirect to the page for the selected option using the redirect_to helper.

这篇关于如何使用选择菜单和link_to重定向到另一个页面-Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆