How to create a simple Rails app with only a controller /view page ,avoiding all the unnecessary files in my app - ruby-on-rails

I have a Rails app which contains a single controller(with a method) and a single view page .I would like to avoid all the unnecessary files in my app to keep this in a simple way. I need an app with controller,routes and views. So how should I scaffold my Rails 3 app, so that it contains only a minimal information?

If you are working on a real simple app, probably you the best fit would be sinatra
That is not rails, but implement with ruby.
HTH

You can manually create the files, especially when you only need a small portion of what the scaffold would create.
1) Add a statics_controller.rb to the app/controller directory. If you literally only need one page, you can simply use the index action (name your method index).
2) Add an index.html.erb (or .haml) to the app/views/statics/ directory.
3) In your config/routes.rb add the line get '/statics' => 'static#index'. You can append , as: 'your_preferred_url if you want to define your own url.
In all of the above, replace "static" with whatever name you think is appropriate. You would also need to add a model and migration if you plan to interact with a backend database.
I personally feel its good to not use scaffolding initially when you are new to rails so that you fully understand what they are doing. Scaffolding is not really doing anything fancy or magical. Its often just creating empty files in the correct directories (like I outlined above).

You can use the rails scaffold to be very specific in which part of MVC you create. For example,
$ rails generate controller Comments
or
$ rails generate model Comment commenter:string body:text post:references
I would highly recommend reading the entire Rails Getting Started Guide. But there is a specific section on generating a controller with scaffold.

Since Rails 6.1. running rails new --minimal gives you an application without all the bells and whistles like action mailer, action mailbox, action text, action job, active storage, action cable, ... and the accompanying configuration and stub files. It then has an ApplicationRecord, ApplicationController and the basics for an HTML view.

Related

where are the auto generated routes in rails scaffolding?

Alright, I'm starting to learn rails and so far im really turned off by how much is auto generated and happening behind the scenes without me knowing. I generated scaffolding for posts. and it auto created routes allowing me to edit and see posts (/posts, /posts/:id/edit, /posts/:id/show....etc) When I go into config/routes.rb I see absolutely no mention of these routes. even though they work. Where are these routes? and where can I add custom routes if the ones for the controller are not in routes.rb?
When you run rails generate scaffold post, rails will generate models, controllers, tests, routes, stylesheets etc.
Rails tells you what files it just generated, you can see it in terminal.
In routes.rb there will be a line
resources :post, this is a shorthand for all RESTful actions that were generated in the controller.
You can declare custom routes in the routes.rb file. I.e.:
get 'my_path'=> 'my_controller#my_action'
In General, Rails can do a lot of stuff for you, and you can avoid repeating default behaviour over and over again. But you can also do most stuff yourself, without Rails magic.
Instead of using scaffolds, just run rails generate controller controller_name action1 action2 (..). You'll end up with just a controller, no automatic views, no automatic model etc.
Or you can just create all files and register your components yourself.
The Rails Guides are a good starting point for understanding the magic.

Ruby on rails without using scaffold and generator?

I am new user to ruby on rails.
I have some question please give the answer as early as possible
1) Is it possible to create web application without using *rails new application_name* command? means creating required folder and file manually?
2) I want to create application without using scaffold and generator, so everything is created manually...I searched but not get a link to do it...
You really should be using rails new (appname) to generate your project directory.
From there, you do not need to generate a scaffold. If you want to go slightly less abstract and create some things manually you can use rails generate resource (resource name).
If you want to go even less abstract then that, you can use rails generate model (model name) and rails generate controller (controller name) and rails generate migration (migration name). Within this level of abstraction, you can specify options such as methods you want the model to have or columns you want the migration to add.
And the least abstract(most manual) would be if you make these files yourself (like actually going in and creating new folders/files for models, controllers, etc.)
So on an order from most abstract to least:
1) generate scaffold
2) generate resource
3) generate model/controller/migration
4) creating the files/folders without rails
Most developers are usually working with the #2, #3, or #4 layers (remember it is always a tradeoff between eliminating a lot of time off by not having to manually create the same code over and over again and flexibility).
rails new app-name creates tons of files and folders which are necessary for the app to run. You would waste tons of time writing them yourself. Read as "reinventing the wheel".
You can simply create a controller and view file. Add the corresponding route. Start the server. Voila. You will have it. Most intro-articles showcase Scaffold to show the power of Rails i.e how much can be achieved with few lines of code.

Rails generate scaffold creates blank controller

The last couple of times that I've used 'rails generate scaffold [ModelName]' everything has been generated except that the controller is blank. It contains no methods at all. It's easy enough to copy that in from other sources, but I'm wondering what is going on.
The only unique thing about this application for me is that it's using the ActiveAdmin gem.
Any suggestions for how I could get this working as expected again?
+1 to hajpoj, but there are a couple additional steps you could use to troubleshoot.
What does rails generate scaffold_controller give you? My first suggestion to be to isolate the controller generator and go from there.
Following that, I would actually look in the Rails generator code at the point of controller generation and work backwards from there. Here is (I believe) the entry point, from there, you can follow the code to where things are failing. This is, obviously, not the easiest path, but would probably teach you a lot about the rails internals.

What is the difference between default scaffold and nifty:scaffold?

I am trying to generate a layout which I can use between my rails webapp and mobile versions. I have been using nifty-generator, but it says the generated files are identical to what was generated by rails3 new application creation.
What's the major difference between default scaffold and nifty:scaffold?
If you visit the Github page (https://github.com/ryanb/nifty-generators) for the project under 'Troubleshooting and FAQs' it answers a few questions including this one. The response given there is:
One of the primary differences is that nifty:scaffold allows you to
choose which controller actions to generate.
rails g nifty:scaffold post name:string index new edit
There are a few changes to the generated code as well, such as no XML
format by default.
It also offers support for HAML, Shoulda, and RSpec.
Once you get a handle on the code that Rails needs in its RESTful controllers I would highly recommend using Inherited Resources (https://github.com/josevalim/inherited_resources) instead. It really helps DRY up your controllers.

Rails: renaming a controller and corresponding model

Is there an easy way to rename a controller and model in my app and all the instances in the corresponding code?
I'm using textmate, would this be as simple as using the replace function and replacing the word Post with Report?
You need to change the name of the Controller and the associated Model, Views, Helpers, Tests & Routes directories, file names, class names & the names in the class definitions.
I found two ways to do this but before you try anything I recommend you back-up your app, preferably with a Software Version Control system like Git & Github.com.
Your first option is to do it manually & there is a good explanation on how to do this here: How to rename rails controller and model in a project
Another way is to destroy your controller & model, and then generate a new one, this will remove all the files which were generated the first time round & replace them with new ones. Michael Hartl explains this solution well in his online guide to Ruby on Rails here: http://ruby.railstutorial.org/chapters/static-pages#sidebar-undoing_things
This is the solution I followed when I needed to make this change to my app, I needed to replace a MVC scaffold I generated called board with a new one called product.
1. First
I made a back-up of the work I did in the layout of the board view, app/views/boards/index.html.erb
2. Then
I ran the below rails commands in the terminal window.
$ rake db:rollback
$ rails destroy scaffold board name:string description:text image:string price:decimal
$ rails generate scaffold product product_type:string name:string description:text image:string price:decimal
$ rake db:migrate
3. Finally
I copied my backed-up boards/index.html.erb file into the newly generated app/views/products/index.html.erb & did a find & replace in my text editor on this file to replace board with product.
I think the second option is much more reliable & quicker but it’s important to make this change early on in your project before you make too many manual changes to the code. It would be better to just take a little more time planning your MVC names & database tables properly before you start your project.
You can also use rails_refactor gem to rename controller, model, etc
for more info check https://github.com/jcrisp/rails_refactor
To rename controller and model use this gem https://github.com/jcrisp/rails_refactor
If you are using textmate, use 'command-shift-f" to look for a string throughout your entire project.
Yes and no. You can rename it that way, but you'll also need to rename the files as well or Rails won't know where to look for the files corresponding to the new Report model/controller/etc.

Resources