Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 Rails将表单发布到漂亮的URL_Ruby On Rails 3_Routes_Rails Routing - Fatal编程技术网

Ruby on rails 3 Rails将表单发布到漂亮的URL

Ruby on rails 3 Rails将表单发布到漂亮的URL,ruby-on-rails-3,routes,rails-routing,Ruby On Rails 3,Routes,Rails Routing,有一个“我显然做错了什么”的时刻。感觉就像我在尝试做一些基本的事情,与框架抗争,所以我请求帮助 我使用的是Rails 3,对于如何创建一个搜索表单,从而生成具有干净URL的页面,我感到有点困惑 我的应用程序允许您搜索从任何位置到另一位置的路线 例如,有效的URL应该是/routes/a/to/B/或/routes/B My routes.rb: match 'routes/:from/to/:to' => 'finder#find', :as => :find match 'rout

有一个“我显然做错了什么”的时刻。感觉就像我在尝试做一些基本的事情,与框架抗争,所以我请求帮助

我使用的是Rails 3,对于如何创建一个搜索表单,从而生成具有干净URL的页面,我感到有点困惑

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

例如,有效的URL应该是/routes/a/to/B/或/routes/B

My routes.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 %>
控制器:

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_标记中使用:method=>'get'时,应用程序可以工作,但URL非常糟糕。当然,使用:method=>post时,变量不再可见,这不利于书签。发布表单后,如何告诉Rails使用漂亮的URL


我是Rails的新手,非常感谢您的耐心。

您的路线会自动命名,您可以通过键入rake routes查看。例如:

new_flag GET    /flags/new(.:format)      {:action=>"new", :controller=>"flags"}
您可以使用new_flag_path或new_flag_url引用路径

你的表格标签条目有点乱。除了使用单独的find方法之外,您还可以使用index方法,但这是您的选择

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

因此,我将更改您的代码,使其如下所示:

routes.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

请发布您的控制器代码。完成。。。。。。。。。性格限制,哦,孩子。非常感谢。我完全理解你回答的逻辑。我正试图实现它,但我似乎做错了什么。当我提交表单时,它只是返回到同一页。表单中生成的HTML看起来根本不正确,它只是在rails控制台上运行FinderControllerindex。表单代码是find_path,:method=>:post do%>我有post'routes'=>'finderfind',:as=>:find in routes.rb。有什么想法吗?没关系,找到了。需要是:post do%>-否:url。