This question already has answers here:
How to rename rails controller and model in a project
(8 answers)
Closed 7 years ago.
Is there an easy way to rename a controller? The only way I know of is to either do it by hand or generate a new controller move the code over and destroy the old one. Seems like there has to be a programmatic way to do this.
Some IDE's (like IntelliJ's RubyMine) will let you Refactor -> Rename a file/variable/method etc, although it's not as reliable in a dynamic language like Ruby as it is in a language like Java.
I had just generated a controller and so I did not have an associated model or database table. I decided to just rename all the files and relevant content that was created when I generated the controller. It is not an 'easy' way to rename the controller but I had confidence in my knowledge of what had been created and what I needed to refactor.
There is a good guide on the ruby on rails guides websites that shows what is generated and what you need to edit or you can see what a typical controller generates below:
$ bin/rails generate controller Greetings hello
create app/controllers/greetings_controller.rb
route get "greetings/hello"
invoke erb
create app/views/greetings
create app/views/greetings/hello.html.erb
invoke test_unit
create test/controllers/greetings_controller_test.rb
invoke helper
create app/helpers/greetings_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/greetings.js.coffee
invoke scss
create app/assets/stylesheets/greetings.css.scss
Also, don't forget to edit the contents of the files above, things like descriptions in your assets files, controller class names and module names etc.
Related
How do I create a rails Events controller in a different directory other than the default:
app/controllers/events_controller.rb
I need to create in app/controllers/api/events_controller.rb
I created the api sub-directory and did cd in terminal to api. When I created the controller it still generated in the default app/controllers/.
Thanks.
You can namespace your controllers (generated like this: rails g controller API::Events).
Put your controller in the api directory within your controllers directory and name the controller's class like this:
class API::EventsController < ApplicationController
More details discussed here: https://stackoverflow.com/a/9946410/1026898
If that is not what you want to do, rails tends to lean in the direction of not putting that controller in a different directory.
It doesn't hurt anything to do so, it's just a bit odd. The rails generators, by default, are built to put the controllers in the conventional directory.
If you want to change where they are generated you will have to update the generator.
To do that with rails generators:
rails g controller API::Events
I wonder how to create singleton controller in Rails 4.2.
For example rails g scaffold Dashboard will generate dashboards_controller witch in my case has no sense because I need only one Dashboard so dashboard_controller is the thing I need.
I see there is an option -c to specify controller name, however I bet there was something like --singleton but is gone now.
So, the question is, should I use -c to override controller name or the "new Rails way" is to create plural controllers names, like dashboards_controller and then use router to point it to dashboard URL?
I don't know how to do it using a generator, but it's easy enough to generate with a plural name and then change it to singular manually.
Your route will be something like:
resource :dashboard, controller: 'dashboard', :only => ['show']
Your controller class should be renamed to DashboardController and the file name itself to dashboard_controller.rb. The view folder that holds your view files should also be singular - app/views/dashboard
The "Rails Way" is to go with plural controller names by default, but it's fine to use singular controller names when they make sense - which they certainly do in this case.
rails g controller dashboard seems what you are looking for.
$ rails g controller dashboard
create app/controllers/dashboard_controller.rb
invoke erb
create app/views/dashboard
invoke test_unit
create test/controllers/dashboard_controller_test.rb
invoke helper
create app/helpers/dashboard_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/dashboard.coffee
invoke scss
create app/assets/stylesheets/dashboard.scss
I have been tinkering around with a Rails project (actually, trying to get it to pick up view templates from lib/templates, which was successful). Now, however, if I generate a scaffold, I get the view files, the migration, but NOT the model file - it creates a zero length file in app/models, but gives it no content.
What on earth have I done?
The way in which I got the thing to pick up the templates was to add a
config.generators do |g|
block, but even if I comment that out now, I can't get rails g model ... to create a model file.
Any clues? FWIW, if I go into an existing alternative rails project, I can create model files perfectly - it's something to do with this project. And I'd quite like to find out what it is rather than blowing the project away and starting again...
For some reason I can't run a rails generate controller on my machine at the moment, something is messed up but still I need a controller. So decided to create it by hand.
But I can't remember the conventions: when I was saying rails generate controller Therapeutics was it creating a
therapeutics_controller.rb
and a
therapeutics.html.haml
So I can create them by hand?
And what was the class name in the controller? Would it be class TherpeuticsController ?
app/controllers/therapeutics_controller.rb should be
class TherapeuticsController < ApplicationController
# define your actions here..
end
app/views/therapeutics => put in your views here
and that's it. if you need helper or model, simply create it by hand. the rails generators are very convenient, but understanding the conventions and creating it by your own gives you more flexibility.
for better understanding, simple check out the basics http://guides.rubyonrails.org/getting_started.html
Just ran it and this is what I got:
$new_project rails generate controller Therapeutics
create app/controllers/therapeutics_controller.rb
invoke erb
create app/views/therapeutics
invoke helper
create app/helpers/therapeutics_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/therapeutics.js.coffee
invoke scss
create app/assets/stylesheets/therapeutics.css.scss
$new_project
I am trying to make a custom validation class in ruby on rails, however I cannot figure out how to make it modular.
I followed this example:
Validator Class Example
For the second example on that page titled: "Validate object single states with an isolated custom validator" how can I put the PreventFutureDateValidator class into a separate file? I cannot figure out where these files would be placed in Ruby on Rails.
These kindo of files are normally stored in /lib.
It's common to put validator classes in /app/validators/