Ruby on rails 能否将自定义路径附加到';新';铁路线路的路径?

Ruby on rails 能否将自定义路径附加到';新';铁路线路的路径?,ruby-on-rails,ruby,routes,Ruby On Rails,Ruby,Routes,我想做什么: 我正在建立一个系统,其中有不同类型的职位。撇开模型不谈,这个问题是关于路线和控制器的 基本上,/posts/new应该转到排序的索引页,而/posts/new/anything应该查找类型anything,然后构建一个表单来创建一个新表单 我是如何做到这一点的: 请随意忽略此部分,因为我可能完全走错了方向 在路由配置中: map.connect '/posts/new', :controller => 'posts', :action => 'new_index' ma

我想做什么:

我正在建立一个系统,其中有不同类型的职位。撇开模型不谈,这个问题是关于路线和控制器的

基本上,
/posts/new
应该转到排序的索引页,而
/posts/new/anything
应该查找类型
anything
,然后构建一个表单来创建一个新表单

我是如何做到这一点的:

请随意忽略此部分,因为我可能完全走错了方向

在路由配置中:

map.connect '/posts/new', :controller => 'posts', :action => 'new_index'
map.resources :posts, :path_names => { :new => 'new/:type' }
在控制器中:

class PostsController
  # implicit: def new_index ; end

  def new
    @post = class_for_type(params[:type]).new
  end
end
该视图包含查看@post类型的代码,以确定要使用的视图集。事实证明,这让我有了90%的成功率:
/posts/new/quip
确实会将我发送到正确的页面来创建一个quip,以此类推
/posts/new
会将我发送到索引页

问题是双重的

  • 我仍然希望有这样的方便方法:

    <%= link_to 'New Post', new_post_path %>
    
    
    
    但是这现在是无效的,因为
    new\u post\u path
    需要
    :type
    参数

  • 如果可能的话,我想用一条路线


  • 我认为您应该看看rails的单表继承:

    这将使管理POST类型变得更加容易,因为许多模型继承了全局模型,但基于相同的sql库

    对于问题本身,为什么不重新定义新的路径辅助方法? 比如:

    def new_post_path
        { :controller => 'posts', :action => 'new', :type => params[:type] }
    end
    

    类型现在是基于params数组自动给定的。

    我认为您应该看看rails的单表继承:

    这将使管理POST类型变得更加容易,因为许多模型继承了全局模型,但基于相同的sql库

    对于问题本身,为什么不重新定义新的路径辅助方法? 比如:

    def new_post_path
        { :controller => 'posts', :action => 'new', :type => params[:type] }
    end
    

    类型现在根据参数数组自动给定。

    Peter Wagnet的解决方案为我提供了拼图中缺失的部分(
    :type=>nil
    ),这使我可以在一行中完成:

    map.resources :posts, :path_names => { :new => 'new/:type' },
                  :requirements => { :type => nil }
    
    当然,我仍然需要进入控制器并进行修复,以便它从
    :new
    操作中呈现
    new_index.html.erb

    (我想这已经不是一行了。)


    Peter Wagnet的解决方案为我提供了拼图中缺失的部分(
    :type=>nil
    ),这使我可以在一行中完成:

    map.resources :posts, :path_names => { :new => 'new/:type' },
                  :requirements => { :type => nil }
    
    当然,我仍然需要进入控制器并进行修复,以便它从
    :new
    操作中呈现
    new_index.html.erb

    (我想这已经不是一行了。)


    如果您可以接受共享操作,则可以设置以下内容:

    # Routes
    map.new_person '/people/new/:type', :controller => :people, :action => :new, :type => nil
    map.resources :people, :except => [:new]
    
    # Controller
    class PeopleController < ApplicationController
    
      def new
        unless params[:type]
          render :action => :new_index and return
        end
    
        @post = class_for_type(params[:type]).new
      end
    
    end
    

    如果您可以接受共享操作,则可以设置以下内容:

    # Routes
    map.new_person '/people/new/:type', :controller => :people, :action => :new, :type => nil
    map.resources :people, :except => [:new]
    
    # Controller
    class PeopleController < ApplicationController
    
      def new
        unless params[:type]
          render :action => :new_index and return
        end
    
        @post = class_for_type(params[:type]).new
      end
    
    end
    

    说“路由”而不是“路径”更好不,路由是另一个将路径链接到控制器、操作和参数的概念。说“路由”而不是“路径”更好不,路由是另一个将路径链接到控制器、操作和参数的概念。(1)我已经在使用STI。正如我所说,这个模型与这个问题的目的无关。(2) 布局中使用了指向new_post_path的链接(它位于每个页面上),因此参数[:type]甚至可能不存在。因此,当您在这样一个页面上时,我想显示一个索引页面。的确,我可以重新定义helper,只返回'/posts/new',不过我想我应该补充一点,我现有的两行解决方案的主要问题是它太粗糙了。如果我再加上3行黑客来解决这个问题,那么它就变成了4行黑客,如果我知道怎么做的话,这可能是1行路线。(1)我已经在使用STI了。正如我所说,这个模型与这个问题的目的无关。(2) 布局中使用了指向new_post_path的链接(它位于每个页面上),因此参数[:type]甚至可能不存在。因此,当您在这样一个页面上时,我想显示一个索引页面。的确,我可以重新定义helper,只返回'/posts/new',不过我想我应该补充一点,我现有的两行解决方案的主要问题是它太粗糙了。如果我再加上3行黑客来解决这个问题,那么它就变成了4行黑客,如果我知道怎么做的话,这可能是一条路线。