rails singular resource still plural?

邮差的信 提交于 2019-12-02 17:54:25

Yes, that's how it's supposed to be. Quote from the Rails Guide on Routing:

Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers.

http://edgeguides.rubyonrails.org/routing.html#singular-resources

You could fix this by setting the plural of "search" to be uncountable so in config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
   inflect.uncountable %w( search )
end

This should now allow search to only be used

Do you want only one route to be generated for the creation?

If so:

resource :search, :only => :create

The fact that the controller for the REST resource is named searches_controller is a convention (that you can change, by forcing the controller's name in the route with resource :search, :only => :create, :controller => :search, but it does not worth it...).

Is the search really a resource? If it is, then what you a creating is an instance of a model with a type of "search", in which case the plural controller "searches" makes perfect sense.

However, if it's a controller that doesn't have multiple models, then maybe not. In which case, you don't need to define the routes with resource :search you can simply use get 'search/create' to tell the router to answer "search/create" to the 'create' action in your 'search' controller.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!