3

我有一个与这样的用户名匹配的受限路由:

controller :users, :path => '/:username', :as => :user, :constrain => { :username => /^_?[a-z]_?(?:[a-z0-9]_?)*$/i } do
   # lots of nested routes go here
end

当我为此编写 RSpec 测试时(与user_id正常使用相比),所有测试都失败了,因为它“找不到路由”,即使它在服务器上工作正常。

describe "for an invalid request" do
  it "should render a 404 if an associated photo is not found" do
    # give it a bad photo id
    xhr :post, :destroy, :id => "999999", :photo_id => "999999", :username => @photo_owner.username
    # not found
    response.status.should == not_found
  end
end

user_id当我在切换到用户名之前在路由中使用 时,此测试运行良好:

resources :users do
  # nested routes
end

xhr :post, :destroy, :id => "999999", :photo_id => "999999", :user_id => @photo_owner.id

那么我做错了什么,发生了什么变化?

我的服务器控制台显示了这一点,这意味着我应该正确传递所有参数:

Processing by TagsController#destroy as JS
  Parameters: {"constrain"=>{"username"=>/^_?[a-z]_?(?:[a-z0-9]_?)*$/i}, "username"=>"rubynewb", "photo_id"=>"2004-the-title-of-the-photo-here", "id"=>"1797"}
4

1 回答 1

2

:constraints => {...}在您的路线定义中使用。

您传递的参数太多...

"constrain"=>{"username"=>/^_?[a-z]_?(?:[a-z0-9]_?)*$/i}

Rails 无法识别:constrain,因此它和它的内容作为参数传递,而不是由 Rails 路由器处理。

于 2012-02-10T15:17:48.890 回答