【问题标题】:rails form_tag url pathrails form_tag url 路径
【发布时间】:2010-11-14 06:09:11
【问题描述】:

这看起来应该很简单,但它一直给我带来问题。

我有一个从模型中提取的 select_tag。我想要的只是让一个人从下拉列表中选择他们的位置,按提交并将其带到该位置页面。

这就是我所拥有的

<% form_tag installation_path do %>
<%= select_tag :id, options_from_collection_for_select(Installation.find(:all), :id, :name) %>
<div id="button">
  <p>
    <%= submit_tag "Go", :name => nil %>
  </p>
</div>

问题是它当然需要:id,但它不会从下面的下拉菜单中拉出:id

我做错了什么,关于“正确”方法的任何其他建议。

谢谢

【问题讨论】:

    标签: ruby-on-rails forms


    【解决方案1】:

    看起来您实际上想要 GET,而不是 POST 参数。

    form_tag installation_path, :method => :get do
    

    【讨论】:

    • 首先谢谢!使用错误的方法是一个问题。但是,为了使路由正确,它需要是 installation_path(id)。然而,拉出的 id 是来自 collection_select 的数组的 id。有任何想法吗?看起来这个简单的任务真的不应该那么困难。
    • 事情就是这样,您要提交表单,然后转到/installations/:id。相反,你最终会在/installations?id=:id。所以你可以给 Rails 一条路线match '/installations', :to =&gt; 'installations#show'。这条路线的问题是——它与 index.html 冲突。因此,您应该有一条特殊的路线:match '/goto_installation' =&gt; 'installations#show', :as =&gt; 'goto_installation'。然后在您的表格中:form_tag goto_installation_path, :method =&gt; :get do。然后在控制器的前置过滤器中,您可以redirect_to installation_path(:id =&gt; params[:id]) 获取所需的网址。
    • 嗨,我刚刚回到这个话题,事实证明我不能使用“匹配”,因为我还没有运行 rails3。关于如何做到这一点的任何想法。我就是不明白为什么这么复杂。似乎这一直在完成,但我似乎找不到任何指导。提前致谢。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多