As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I know that using plural for controllers name is the right way in Ruby, but in some cases using singular for controllers name is more appropriately. For example
http://foobar.com/admin/login/
http://foobar.com/admin/dashboard/
http://foobar.com/profile/
I think you get what I mean.
So which are the best practices of using singular for controllers names ?
Any example will be appreciated !
If you don't need the whole scaffolding, for example you probably have no admin model, then you can just generate a controller with a singular name.
rails g controller admin
Then, you need to take care of the routing.
match 'admin/login' => 'admin#login'
Rails favours convention over configuration, so it means that controllers are ALWAYS using plural, table names also, whereas models are always using singular, with an uppercase at the beginning.
I would strongly advice you to stick to these conventions, that's best practice and in the foundation of the Rails framework
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What's a good conventional order for associations, scopes, attributes, validations, callbacks, non-standard code (acts_as_*), class and instance methods?
Also, if I need to split a model into modules, is it better to base these modules on features (Post::Comments, Post::Share, Post::Admin etc.) where each module also include the scopes and associations, or is it preferable to keep all scopes and associations in the base model and only split the methods?
You won't get a clear answer on this. It depends on your personal preference and what suits your code.
What I would suggest regarding the order in the model: pick one, and stick to it. It doesn't really matter if callbacks come before or after validations, as long as it makes some sense to you, and you can remember it.
And about the modules: I would pick two of the smaller models, and try both approaches and have a look what suits you better. Personally I don't like my scopes distributed amongst multiple files, but that does not have to be your preference too.
Maybe you want to have a look how others are doing it, e.g. https://github.com/gitlabhq/gitlabhq or https://github.com/diaspora/diaspora to help with your decisions.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I like to use the strongly defined routes like
#Html.RouteLink("Home", RouteName.HomeIndex)
Why there is no equivalent helper for child actions? I don't like the actual #Html.Action(actionName, controllerName, routeValues) helper.
In addition, the performance would be better. There is no need to check all routes internal in the RouteCollection.
Contribute your idea and even the helper at http://aspnet.codeplex.com/ - it's open source.
The answer to Why isn't there one is that either:
Nobody thought of it yet
Someone thought of it but didn't do anything about it
Someone thought of it but thought better of it - for example because a route can be very general and you would need to supply controller, action and routevalues for most routes, which means the Action helper works fine.
In terms of performance, I've not had any problems with the performance of Html.Action so you might want to measure it before you get too concerned about optimising this aspect of the framework.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'd like to build a control panel (let's call it "dashboard") for several applications. Each application has a REST-API which will be used by the dashboard. The dashboard should have a module for every managed application.
In Rails I could use Controller Namespaces to achieve something like this. The models and the views could be separated in a similar way. This would lead to the following directory structure:
controllers/app1/
models/app1/
views/app1/
controllers/app2/
models/app2/
views/app2/
But I don't like that. I would rather have something like this, to have the modules better separated:
modules/app1/controllers
modules/app1/models
modules/app1/views
modules/app2/controllers
modules/app2/models
modules/app2/views
Is something like this possible with Rails 3?
If you want that kind of separation you should investigate using Rails Engines to organize the components of your application. An engine is a sort of sub-application that's mostly self-contained.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Thought I would put this out there before I embark on my own research.
In a current MVC 4 app, I am looking for a way to track/audit visitors. I am looking for a way to:
Record a record on each site request.
Record will include things like logged in user, session id, requested page, page coming from, etc., etc.
Action methods will be decorated with an attribute to trigger the creation of that record, so we can specify exactly which action methods get logged.
Anyone attempted or heard of anything like this?
Thanks in advance for any replies.
An ActionFilter seems to be the way to go. I have an example of a similar requirement that I'd be willing to send you, let me know.
You should consider writing a base controller that all your controllers will inherit from and then do all the logging in there.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In Rails, we offen write a lot of code in our models such as class methods, plugin methods, named_scopes, callbacks... I am wondering if there is a good pattern to organize the sequence. I saw the best example in a presentation before, but now I forgot.
Anyone have suggestion? Thanks
There is no set way... If you are using a scope (since Rails 3, named_scope is deprecated) that relies on a method, it has to be defined after the method in the model. It's possible to mix and match and sometimes it's necessary to do so.
It doesn't affect load time or efficiency to the best of my knowledge
I'm pretty OCD when writing ruby, so I have a very opinionated answer to your question. I created this gist as an example of the structure we use.
I wrote about that a while back as part of my style-guide:
acts_as_good_style.
YMMV as to what the "best" grouping/ordering is, but if you'd like my take it's there under the "Model idiom" section