ruby-on-rails-3 - Rails POST 表单到漂亮的 URL

标签 ruby-on-rails-3 routes rails-routing

有一种“我显然做错了什么”的时刻。感觉就像我正在尝试做一些基本的事情,并与框架作斗争,所以我寻求帮助!

我正在使用 Rails 3,但对于如何创建一个搜索表单来生成具有干净 URL 的页面有点困惑。

我的应用程序允许您搜索从任何位置到另一个位置的路线。

例如,有效的 URL 为/routes/A/to/B/或/routes/B

我的路线.rb:

match 'routes/:from/to/:to' => 'finder#find', :as => :find
match 'routes/find' => 'finder#find'

我的搜索表单:

<% form_tag('/routes, :method => 'post') do %>
  <%= label_tag(:from, "From:") %>
  <%= text_field_tag(:from) %>
  <%= label_tag(:to, "To:") %>
  <%= text_field_tag(:to) %>
  <%= submit_tag("Go") %>
<% end %>

Controller :

class FinderController < ApplicationController
  def index
  end

  def find
    if params[:from].blank? or params[:to].blank?
      render :action => "invalid_results" and return
    end
    @from = Location.find_by_code(params[:from].upcase)
    @to = Location.find_by_code(params[:to].upcase)
    if @from.nil? or @to.nil?
      render :action => "invalid_results" and return
    end

    @routes = Route.find_all_by_from_location_id_and_to_location_id(@from, @to)

  end
end

当我在 form_tag 中使用 :method => 'get' 时,应用程序可以运行,但 URL 很丑陋。当然,使用 :method => 'post' 时,变量不再可见,这不利于添加书签。在发布表单后,如何告诉 Rails 使用我的漂亮 URL?

我是 Rails 的新手,因此感谢您的耐心等待。

最佳答案

您的路由会被赋予一个自动命名的路径,您可以通过输入rake paths来查看该路径。例如:

new_flag GET    /flags/new(.:format)      {:action=>"new", :controller=>"flags"}

您可以使用new_flag_pathnew_flag_url来引用路径

您的form_tag条目有点奇怪。您也可以使用 index 方法,而不是使用单独的 find 方法,但这是您的选择。

您可能会发现使用标准 redirect_to 根据输入重定向到更漂亮的 URL 会更容易。如果您不希望重定向,那么您需要使用 jQuery 动态更改表单的操作方法。搜索通常使用丑陋的 GET 参数。

所以我会将您的代码更改为如下所示:

路线.rb

get 'routes/:from/to/:to' => 'finder#routes', :as => :find_from_to
post 'routes/find' => 'finder#find', :as => :find

_form.html.erb

<% form_tag find_path, :method => :post do %>
  <%= label_tag(:from, "From:") %>
  <%= text_field_tag(:from) %>
  <%= label_tag(:to, "To:") %>
  <%= text_field_tag(:to) %>
  <%= submit_tag("Go") %>
<% end %>

finder_controller.rb

class FinderController < ApplicationController
  def index
  end

  def find
    if params[:from].blank? or params[:to].blank?
      render :action => "invalid_results" and return
    end
    @from = Location.find_by_code(params[:from].upcase)
    @to = Location.find_by_code(params[:to].upcase)
    if @from.nil? or @to.nil?
      render :action => "invalid_results" and return
    end

    redirect_to find_from_to_path(@from, @to)

  end

  def routes
     @routes = Route.find_all_by_from_location_id_and_to_location_id(params[:from], params[:to])
  end
end

关于ruby-on-rails-3 - Rails POST 表单到漂亮的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6970629/

相关文章:

ruby-on-rails-3 - ActionModel 未定义方法 'on'

api - 这是 RESTful 吗?

ruby-on-rails - 基于条件的 Rails 路线

ruby-on-rails-4 - Rails 对 CORS 预检选项请求响应 404

asp.net-mvc-3 - MVC 3 : Routing to Static Pages

ruby-on-rails - 我需要在 rails 路线前加斜杠吗

ruby-on-rails - 我可以在 Rails 3 中以编程方式更改 config.cache_classes 吗?

ruby-on-rails - 神秘的 AbstractController::ActionNotFound (路由就在那里)

ruby-on-rails - 编写 Rails 测试,不要保留那么多数据库对象

c# - 尝试在我的 WebAPI 上调用特定的 GET 方法,但未找到 HTTP 404