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 从路由收集范围/筛选器_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 从路由收集范围/筛选器

Ruby on rails 3 从路由收集范围/筛选器,ruby-on-rails-3,Ruby On Rails 3,我已经为“发票”设置了一个标准的资源丰富的路由设置,但是我希望添加根据发票记录状态过滤发票记录的功能 /invoices - shows all invoices /invoices/unpaid - shows all unpaid invoices /invoices/paid - shows all paid invoices. /invoices/3 - shows invoice #3 我用一个明确定义的匹配路由实现了这一点 match "/invoices/pending" =&g

我已经为“发票”设置了一个标准的资源丰富的路由设置,但是我希望添加根据发票记录状态过滤发票记录的功能

/invoices - shows all invoices
/invoices/unpaid - shows all unpaid invoices
/invoices/paid - shows all paid invoices.
/invoices/3 - shows invoice #3
我用一个明确定义的匹配路由实现了这一点

match "/invoices/pending" => "invoices#index", :state => 'pending'
match "/invoices/:state" => "invoices#index"
然而,随着可能的州越来越多,这意味着定期修改路线,也意味着我重复了很多

我的下一个尝试是在匹配路由中使用命名参数使此路由更具动态性

match "/invoices/pending" => "invoices#index", :state => 'pending'
match "/invoices/:state" => "invoices#index"
但是,这将否定/invoices/id路由,并尝试查找/invoices/3时未找到任何记录,因为它基于状态参数进行搜索


有人可以帮助定义这样一个动态工作的筛选器路由吗?

在路由中添加正则表达式作为约束:

match "/invoices/:id" => "invoices#show", :id => /\d+/
match "/invoices/:state" => "invoices#index"

它应该选择用于显示的唯一编号id-s,其余编号用于索引。

在路由中添加正则表达式作为约束:

match "/invoices/:id" => "invoices#show", :id => /\d+/
match "/invoices/:state" => "invoices#index"
它应该选择唯一的id-s号用于显示,其余的用于索引。

尝试使用路由。差不多

match "/invoices/:id" => "invoices#show", constraint: { id: /\d+/ }
match "/invoices/:state" => "invoices#index", constraint: { state: /\w+/ }
尝试使用路由。差不多

match "/invoices/:id" => "invoices#show", constraint: { id: /\d+/ }
match "/invoices/:state" => "invoices#index", constraint: { state: /\w+/ }

我决定采取一种稍微不同的方法(请原谅双关语),通过添加一个约束,而不是使用REGEX使用一种方法直接在状态机上查看可能的状态

# State based routes, which match the state machine dynamicly.
match "/invoices/:state" => "invoices#index", constraints: lambda { |r|
    # Get an array if potential state names.
    states = Invoice.state_machine.states.map &:name

    # See if the request state name matches or not.
    states.include?(r.params[:state].parameterize.underscore.to_sym)
}

# Resource routes go here.
这基本上意味着,如果我现在查找/invoices/paid或/invoices/unpaid,我将得到预期的索引操作,状态变量设置为expected

但是,执行诸如/invoices/pay_soon之类的无效事件会返回404


对此解决方案非常满意,但多亏了其他建议,我才开始使用约束。

我决定对此采取稍微不同的方法(请原谅双关语),添加一个约束,而不是使用REGEX使用一种方法直接在状态机上查看可能的状态

# State based routes, which match the state machine dynamicly.
match "/invoices/:state" => "invoices#index", constraints: lambda { |r|
    # Get an array if potential state names.
    states = Invoice.state_machine.states.map &:name

    # See if the request state name matches or not.
    states.include?(r.params[:state].parameterize.underscore.to_sym)
}

# Resource routes go here.
这基本上意味着,如果我现在查找/invoices/paid或/invoices/unpaid,我将得到预期的索引操作,状态变量设置为expected

但是,执行诸如/invoices/pay_soon之类的无效事件会返回404


对此解决方案非常满意,但感谢其他建议使我走上了使用约束的轨道。

感谢这些建议,起初我认为这很有效,但后来我注意到,这打破了我的其他资源路线,如按外观顺序评估的“新建”和“编辑”路线,如果您按正确的顺序指定,应该可以。例如,资源路径是第一个,然后是这两个。感谢您的建议,起初我认为这工作得很好,但后来我注意到,这破坏了我的其他资源路径,如“新建”和“编辑”路径,按外观的顺序进行评估,如果您按正确的顺序指定,应该可以。例如,首先是资源路径,然后是这两条。