I created model for user in ruby on rails using scaffold.
Then I got to know that when I saw controller's folder of the project I would find usercontroller.rb file created ? Does this means that whenever model is created controllers are created with it ?
It isn't the model creation that does it, it's the scaffolding.
http://guides.rubyonrails.org/v3.2.9/getting_started.html
Section 6 of that document describes what is generated during scaffolding. The scaffolding process creates a number of files, controller being one of them.
rails generate scaffold will generate a model, controller database migration, and views.
Here is a list of the generators that Rails provides:
assets
controller
generator
helper
integration_test
jbuilder
mailer
migration
model
resource
scaffold
scaffold_controller
task
The Ruby on Rails guides can provide you with additional information on the command line tools.
Related
I am following the Rails Tutorial. It is starting to generate a scaffold. I just don't know for sure where to pit it. In a subdirectory or right at the root. My hunch is the root. i.e. JimJones$ not JimJones$/work/newapp. The apps can keep[ changing so I would imagine I would install the scaffold at the root. Any help would be greatly appreciated. Thanks, Chris
Your hunch is wrong, you should be in your /newapp directory, assuming that's the name of your app.
If you're not sure where you're at, enter pwd at the command line, and this will
print out your current location. If it ends in /newapp, you're all good.
rails generate scaffold myscaffold creates a set of MVC scaffolds called myscaffold(s), INSIDE your current app. In fact, You shouldn't be able to run rails generate scaffold unless you are inside a rails project.
Scaffolding is specific to Rails applications. First create a new Rails application using rails new appname command. Then move to the appname directory.
Then use scaffolding
rails generate scaffold test
The above command will generate a set of model, database migration for that model, controller, views, and test suites for the resource test
More info on scaffolding : http://guides.rubyonrails.org/command_line.html
You need to be in the rails app directory to use scaffold and the code generated by scaffold will be put in appropriate locations in your rails app directory e.g. model in models directory controller in controllers
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.
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.
I know it is not recommended but is there a way to generate/modify an existing project so it doesn't create test files when using generators such as rails generate controller NAME?
ruby script/generate controller Account --no-test-framework
Check out the API.
I have recently downloaded a new project(open source),i found certain features missing like blog,forum ,chat etc.. ..so I like to add those features to the project .My problem if run rails forum it will create a new rails project but i want to add to the existing project. I have found business logic. . . .
I had created models
ruby script/generate model forum
ruby script/generate model topic
ruby script/generate model post
rake db:migrate
ruby script/generate migration add_foreign_to_topics forum_id:integer
ruby script/generate migration add_foreign_to_post topic_id:integer
rake db:migrate
Then i ran
ruby script/generate controller forum
it was asking should i overwrite or not,so i am stuck up here,i need to create a controller and view for this feature.I am following this tutorial http://net.tutsplus.com/tutorials/other/building-a-forum-from-scratch-with-ruby-on-rails/ and i have already user table etc..
As far as i can read, you are not following the tutorial, as it does a scaffold which generates the controller and models at the same time.
Either you do something like
ruby script/generate scaffold Forum title:string contents:text
and it generates the model, controller, routes and views for you. In the tutorial they use nifty_scaffold and i think it mostly improves the view.
If you create the models seperately, you need to do something like
ruby script/generate controller Forum index show create edit update new destroy
and then you will have to fill in all those actions yourself. You will also have to set your routes correctly. That is not bad and not at all difficult. But when you are starting, using the scaffold is much easier.