Rails 能不能用表单的方式不经过查询直接跳转到 show 页面?

lb563 · March 26, 2012 · Last by doitian replied at March 29, 2012 · 2791 hits

以前都是先输入要查询的 id 然后提交到 search action 由 search action 来处理如果没有的找到的话就给提示等 . 现在想输入 id 后提交表单直接转到 show 页面不经过 search 了. 实践了一下,表单没有写出来。请教大家.这个表单应该怎么写呢?

form_for resource, :url => {:acton => :show}, :method => :get do |f| f.text_field :entry f.submit "Go" end

不明白什么意思,楼主能详细解释一下吗?

<form action="/posts/show" method="get">
  <input name="id" />
  <button type="submit">Go</button>
</form>

还可以考虑加入 JS 处理

#3 楼 @huacnlee 确实。用 js 来的方便点。

  1. 加个路由 match 'resources/show' =>'resources#show', via: :get
  2. 表单里的 text field 用 id 做 name

然后你表单提交后就到了 /resources/show?id=10,因为url里的params和query都会merge到params里,所以show方法不用做任何改动

You need to Sign in before reply, if you don't have an account, please Sign up first.