Parameters for generating controllers in ruby on rails? - ruby-on-rails

When using the command:
rails generate controller home index
When generating a controller do we always have to have a view be generated? Also, can a controller and a view be generated on its own?

No, you do not always have to generate the action. In your case, if you just wanted to generate an empty controller, you can have:
rails generate controller home
Alternatively, controllers and views can be generated manually.

Maybe you're are getting the scaffolding wrong. It should provide you a basic skeleton for beginning and in the later advances on your work, you will start creating it by yourself.
Every time you need to create something custom means you are advancing so scaffolding will stop fitting your needs.

Related

Creating Methods to an existing Controller via Terminal

I have an existing model that I'd like to add a number of methods to. While I could manually add the methods to the controller as well as its corresponding style/script templates and routes I'd much rather have them done just as I would've when I created the controller in the first place:
rails generate ControllerName hello goodbye
Is there any way to accomplish this?
I think this is a perfectly valid thing to want to do. However the general concensus seems to be that it is too trivial hence why there is no option for it.
A couple of things you could do though:
If you are only adding one or two actions to an otherwise complex controller you could generate another dummy controller with just those two actions. Then copy over the new bits to your existing controller and trash the dummy one. Something like git is great for keeping a track on what the generator changed here.
Alternatively if you want to add a lot of new actions to a trivial controller you could save off the original files, let the generator overwrite the existing controller and then put back the original bits.
Remember you can always use the --pretend option on the generator to see what it would create without actually doing it. This can be useful especially to see what view files it would create for the actions in your case.
You can do
rails generate controller mycontroller hello goodbye
This would generate controller called mycontroller with methods hello and goodbye.

How to create own generator in rails 4

I want to create own customized generator for scafflod so that i can change the default layout of generated .html.erb or .html.haml for a particular application.
I want to customize the layouts
Making generators is a good idea when you want a specific layout applied to few of your resources. It might be like you have an normal user section and admin user section with different layouts.
You may have a good reference of making a generator from here:http://railscasts.com/episodes/218-making-generators-in-rails-3?view=asciicast
There is some pretty nice stuff you can do with this. Problem is it is very badly documented, but once you know where to store the various template files, you're on the move:
It all starts in lib/templates
lib/templates/active_record/model/model.rb contains the model template
lib/templates/factory_girl/model/fixtures.erb contains the factory girl template
lib/templates/rails/scaffold_controller/controller.rb the controller
lib/templates/rspec/model/model_spec.rb the rspec model
lib/templates/rspec/scaffold/controller_spec.rb the rspec controller
lib/templates/haml/scaffold/_form.html.haml the views (idem dito for edit, index, new and show)
If you need more info on the templates, just let me know!

Create a new action for existing controller

I know this is probably a newbie question, but is it possible to create a new action (method in controller & associated view) from the command line on an existing controller?
For example, I already have a controller named 'Products'. Could I run:
rails g controller products [new_action]
and then rails would insert:
def [new_action]
end
Into my products controller and create a new file called '[new_action].html.erb' in the views/products/ directory? I have already googled this, but no satisfactory answer was returned. Also, I would just go ahead and try it, but I am pretty far into the development of my current app and really do not want to mess anything up.
I'm pretty sure you won't be able to do this in a 100% automated way. The reason is that Rails doesn't know what you've done with your routes or controller, and it would require some logic to know how to update these existing files. Your best bet is to just add the new action manually. Add the new method to your controller, update your routes file, and add the view. It will probably take 1 minute at most. Also, if you're not using version controller (which your question eluded to), then you don't have to worry about it automatically overwriting something.
we can create manually the action in the controller and view but you should also add test statements that because should be good automated process, something like rails generate controller NAME [action action] option m
m = merge
Rails provide posibility of creating custom generators (but this is more advanced subject), which can be tailored for your needs.
More info:
http://guides.rubyonrails.org/generators.html

creating a simple html page in ruby

Following the ruby tutorial I am trying to create a simple html page.
I will need a controller, i think for some presentation manipoulation but not DB.
I thought that I would create a model, controller and view but then I see that
rails generate model mainMenu freeData1:string freeData2:string
creates the db script.
In order to achieve a simple managed html page that would not require db, what should I create ?
controler only? what is the best practice?
what methods should I put in it for it to be displayed?
thanks.
Yes, you'll just want a controller for the pages which will also create a views folder for your new controller that you can put HTML/ERB/whatever files in.
Since youre not working with the db, you can probably skip the need of a model.

Rails Routes - How to add a route of an object without a controller

I created an object named settings. So i also provided its route in the routes.rb file i wrote "map.resources :settings". Now as i'm trying to save to the database with that object, it keeps on getting to the localhost:3000/settings url, which i don't have. i', also having this error
NameError in SettingsController#create
uninitialized constant SettingsController
PLEASE HELP! THANKS!
I am not 100% sure, but I believe you need to have a controller to add a route. Check out this diagram: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:mvc
If you use Rails, you have to stick to its rules. Rails implements the MVC pattern, where the controller has the role to provide the linking between a request started in the client web page (view) to creating, reading, updating and deleting (CRUD) objects (== models). The routes.rb define here the mapping from the URL to controller actions, not directly to the resources. See the "Rails Guides for Routing" for more information.
If you want to use your model objects, Rails provides an easy way to start that: scaffolding. By using rails generate scaffold setting <attr_name1>:<type1> ..., you are able to create the following:
A migration for the database that creates the settings table.
Generation of the model object Setting that maps to the created database table.
A controller SettingsController that allows CRUD for your model objects.
View files for the actions generated for the controller.
You can all do that by hand, but it is a good starting point to begin with. And read the basic tutorials and play with the example applications to get a feeling for Rails ...

Resources