简体   繁体   English

在rspec中命名自定义路由

[英]Named custom routes in rspec

I'm trying to write a request spec for a custom action on one of my controllers. 我正在尝试为我的一个控制器编写自定义操作的请求规范。

My routes.rb is like this: 我的routes.rb是这样的:

controller :profile, :path => 'profile' do
  match 'view_friends/:circle_id', :to => :view_friends, :via => [:get], :as => 'view_friends'
end
resources :profile

I want to visit this action, and was hoping that as I can use 我想访问此操作,并希望可以使用

visit profile_path
visit new_profile_path 
etc

That I could do 我能做的

visit view_friends_profile_path 

However this gives me the "error undefined local variable or method " 但这给了我“错误未定义的局部变量或方法”

I can get the desired behavior by writing 我可以通过写得到想要的行为

visit profile_path.to_s + '/view_friends/' + circle.id.to_s

But that is horrible. 但这太可怕了。 What am I missing to be able to name a custom action? 我无法命名自定义动作?

edit: 编辑:

Relevant output from rake routes 耙路的相关输出

   view_friends GET      (/:locale)/profile/view_friends/:circle_id(.:format)       {:controller=>"profile", :action=>"view_friends"}

Firstly I'm a bit confused. 首先,我有点困惑。 Usually in rails when you make a controller, the controller has a plural name. 通常,当您制作控制器时,控制器会使用复数名称。 For instance if your model name is Profile , then you would have a ProfilesController . 例如,如果您的模型名称是Profile ,则将有一个ProfilesController The way you are writing your question implies that your controller is named ProfileController . 您编写问题的方式意味着您的控制器名为ProfileController Seems kind of unusual, so if you could clarify that would be helpful. 似乎有点不寻常,所以如果您能弄清楚那会有所帮助。 That being said, try this: 话虽这么说,试试这个:

# in routes.rb
resources :profile do
  member do
    match 'view_friends/:circle_id' => :view_friends, :via => :get, :as => 'view_friends'
  end
end

Should produce what you want. 应该产生你想要的。 You can access it now by doing this: 您现在可以通过执行以下操作来访问它:

view_friends_profile_path(profile_id, circle_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM