I have an application that has multiple controllers that those controllers have within them 2 routes #Controller(['1/:someParam', '/2/:someParam']) but when I create swagger it shows both of them within the same group, what I need is to be able to group those as well.
Is there any possible way to create such a thing?
You can use ApiTags.
#ApiTags('firsturl', 'secondurl')
ApiTags accept string array as parameter.
Related
Are rails helpers the same as methods. Aren't they just custom to the views?
I know that methods can be made within the controllers but the way I have been understanding that helpers are custom made methods that can be called upon and used within the views.
Helpers are functions defined in the helpers directory, and they are different from the controller actions.
Let's say, you are creating a form in a view, and you want this form to have a drop down menu to all countries in the world, then you would define a helper inside helpers directory that contains all the countries and then call this helper inside your view, instead of listing the actual countries in your view.
Controller actions on the other hand are different, they are also functions like helpers, but they perform operations like create a new record in the database, or delete a record, and so on.
Read through http://guides.rubyonrails.org/action_controller_overview.html for a deeper understanding.
I'm quite new to Rails development and I came up with this question today. I have a method that returns some JSON data.
It is used to populate a select with a list of cities according to what was selected on a previous select (list of states). So, it's a simple method that loads a list based on some ajax parameter passed through and it is used all along my site.
I'm using Rails 4 and I placed this method on my HomeController. So everytime I need to fetch the list of cities, I call the HomeController to load the data.
Is this the correct approach or should I place this method on a more generic controller (like ApplicationController)? Is there a better way?
I think the best thing is to keep this modular. So you can create a separate controller for this, like StatesController - and possibly even a separate model if that makes sense for your application (I'm not sure where you're getting your data). There is no cost to having extra controllers, and this way your code is clean and organized, with each piece of functionality existing in its logical place.
I'm working on a Web API with MVC4, and I'd like to make it backwards-compatible, as I don't control when the clients are updated.
In order to do that, I'm going to create controllers on different namespaces, something like MyApp.Controllers.v1_0.AccountsController and MyApp.Controllers.v1_1.AccountsController
Obviously, when I create both of them and try to access to an action, I get "Multiple types were found that match the controller named 'Accounts'"
Then, what I tried to do is writing my own IHttpControllerActivator, so that when Create is invoked, it returns one of them... but that doesn't work b/c it never gets hit, which makes sense as Create receives an System.Web.Http.Controllers.HttpControllerDescriptor that includes information about the controller it's about to use.
Also, I can't just name the controllers different (Accounts1_0Controller, Accounts1_1Controller), as when the activators returns Accounts1_0Controller, it says that its name is not "Accounts"... it probably gets its name as Accounts1_0.
Do you see any way of either:
Set the namespace from the url? so that I have the url /v1_0/SomeAction or /v1_1/SomeAction and it searches the controller on the appropriate namespace
Having multiple controllers with the same MVC name but different class name?
Hope the issue is clear enough.
Thanks!
Fixed it implementing my own IHttpControllerSelector. It's great how Microsoft made MVC4 open source, made it almost painlessly looking at their System.Web.Http.Dispatcher.DefaultHttpControllerSelector.
I have a class, called Tab, that needs access to params so that it can check params[:controller]. Two questions? (1) Where should I put these two classes, Tab and Tabs? Should they be in lib? Or in application_helper.rb? (2) What's an easy way to get access to params in that class? Should I just add a params parameter to the constructor of Tabs and pass params in at the call site?
1) Where should I put these two classes, Tab and Tabs? Should they be in lib? Or in application_helper.rb?
it can go to lib, ideally we keep all custom classes/modules in lib directory.
2) What's an easy way to get access to params in that class?
I think you are right here, you should add a params parameter to the constructor of class and pass params in it.
I would like to create a checking tool/rake task, that would dynamically go through all the public methods that are possibly accessible from outside world and check our authorization rules.
For this I would need to collect all the controllers and all its public methods. How to do that? I would like to use Ruby reflection or metadata techniques rather than grepping files.
AFAICT this is impossible to do in the general case as you can't tell which params go where, e.g. /items/foo/id or /items/foo?bar=baz ? Also, which values of the params are ok ?
But you can get a decent value with
Rails.application.routes.routes.map(&:path)
which shall give you a list in the form used in rake routes (/klass/:id/action(.:format))