I have been struggling to understand why I can't get view files created for my Rails project. The documentation shows that generating a controller also generates an associated view:
Rails Docs
If I use that same command, I get one file generated, being the controller.
My command
The only two files within the Views folder in my Rails project are mailer.html.erb and mailer.text.erb
Am I doing something wrong? Do I manually create the view files, and if so I can't seem to get them "connected" to the associated controller. I am new to Rails, so any insight would be helpful. Thanks!
try this command :
rails g controller Articles index create
You can create methods and views dynamically after If you typed controller name in command.
you are creating only a controller in your command not any methods that's why the views are not generated. If you need to generate views for the particular methods then you may run the below listed command.
rails g controller Articles index show
Here, index, show are the methods name. So, this command will create ArticlesController and also the respective view files.
You can use the scaffold option.
rails g scaffold Post name:string title:string content:text
This command will generate the following files.
File
Purpose
db/migrate/20100207214725_create_posts.rb
Migration to create the posts table in your database (your name will include a different timestamp)
app/models/post.rb
The Post model
test/unit/post_test.rb
Unit testing harness for the posts model
test/fixtures/posts.yml
Sample posts for use in testing
config/routes.rb
Edited to include routing information for posts
app/controllers/posts_controller.rb
The Posts controller
app/views/posts/index.html.erb
A view to display an index of all posts
app/views/posts/edit.html.erb
A view to edit an existing post
app/views/posts/show.html.erb
A view to display a single post
app/views/posts/new.html.erb
A view to create a new post
app/views/posts/_form.html.erb
A partial to control the overall look and feel of the form used in edit and new views
test/functional/posts_controller_test.rb
Functional testing harness for the posts controller
app/helpers/posts_helper.rb
Helper functions to be used from the post views
test/unit/helpers/posts_helper_test.rb
Unit testing harness for the posts helper
app/assets/javascripts/posts.js.coffee
CoffeeScript for the posts controller
app/assets/stylesheets/posts.css.scss
Cascading style sheet for the posts controller
app/assets/stylesheets/scaffolds.css.scss
Cascading style sheet to make the scaffolded views look better
Related
I've seen some Rails examples where generating a controller also creates the view file (not just the view folder).
In section 6.4 of Rails Guide, it shows an empty view folder and no view file. This is what my local installation is doing. I don't get any view files.
Is there some way to have Rails auto generate the view file when running rails g controller ...? Or, is it more likely the person created the view file manually and didn't show that part?
Use rails generate scaffold instead which will generate the model, view and controller files for you in a single operation.
If you want to create the models, views, and controllers for a new
resource in a single operation, scaffolding is the tool for the job.
e.g.:
rails g scaffold Post name:string title:string content:text
But, if you really want to use rails g controller and also create the view files for you, then you have to specify the action names for your controller:
rails g controller Controllername index show edit update
This way, it will create the view files for these four actions: app/views/.../index.html.erb, app/views/.../edit.html.erb . . . etc.
But, looking at your case, you should use scaffolding as it will do a lot of work for you.
To generate basic view and controller actions you should run e.g.: rails g controller Controllername index show
Basic view for index and show action will be created.
I've got the same problem if i create an api-project with RubyMine (New Project > Rails Api Project). The first project was created on cli (rails new), ze second in RM:
C:\Projekte\railstest3>ruby bin/rails generate controller Welcome index
create app/controllers/welcome_controller.rb
route get 'welcome/index'
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/welcome.coffee
invoke scss
create app/assets/stylesheets/welcome.scss
C:\Projekte\railstest3>cd..
C:\Projekte>cd railstest
C:\Projekte\railstest>ruby bin/rails generate controller Welcome4 index
create app/controllers/welcome4_controller.rb
route get 'welcome4/index'
invoke test_unit
create test/controllers/welcome4_controller_test.rb
C:\Projekte\railstest>rails -v
Rails 5.0.2
Solution is to create new "Application Projects" and not new "Api Project" in RM.
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
New to Ruby on Rails so this may be a stupid question. I have an app and I can bundle my gems without issue. So now I want to add some mostly static pages. I try to generate a controller for them with rails generate controller MostlyStatic page1 page2. This should generate a controller named mostly_static and pages named page1 and page2. Instead, I throw an error. Apparently the generate command is trying to connect to the database, which I have not yet created. There is nothing in these pages that should be a database table, so I'm a bit confused as to why the database is being brought into the process at this juncture. I've looked through various tutorials and none say that a database is required to generate controllers for static pages. So... what am I missing? Do I need to create the database first just to generate static pages? And, if so, will subsequently dropping any tables created by that generation impair the function of my app? I really don't want a bunch of useless tables for static pages hanging around. Is there a way to generate these pages and controllers without the database?
You are not following the convention for generating controllers. Generating a controller will not create a database table. You have to do that by calling rails generate model, rails generate resource or rails generate scaffold.
So you want a controller for a few static pages. Try this
rails generate controller static_pages home help contact
Notice the generator is plural and snake case (static_pages). this will generate the static controller and the home.html.erb, help.html.erb, and contact.html.erb pages
Now you can access the pages with these actions in the controller
def home
end
def help
end
def contact
end
Also need to make sure the routes are set up
# routes.rb
match '/home', to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
No database is set up and you can visit the pages. Thats all you need to do. just follow the conventions,like plural controllers and singular models and rails takes care of the details. Hope this gets you started
UPDATE
in response to the comments here is the standard output of generating a controller. Note my example used haml instead of erb, but there is nothing related to the database in the output.
rails g controller static_pages home help contact
create app/controllers/static_pages_controller.rb
route get "static_pages/contact"
route get "static_pages/help"
route get "static_pages/home"
invoke haml
create app/views/static_pages
create app/views/static_pages/home.html.haml
create app/views/static_pages/help.html.haml
create app/views/static_pages/contact.html.haml
invoke rspec
create spec/controllers/static_pages_controller_spec.rb
create spec/views/static_pages
create spec/views/static_pages/home.html.haml_spec.rb
create spec/views/static_pages/help.html.haml_spec.rb
create spec/views/static_pages/contact.html.haml_spec.rb
invoke helper
create app/helpers/static_pages_helper.rb
invoke rspec
create spec/helpers/static_pages_helper_spec.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss
For anyone stumbling across this question, the correct answer is that the database need not exist, but it must be properly configured as if it did exist in the config file. Generating the controller does not actually create the database.
I am really new to rails and working to customize a project. I generated a new scaffold, called it New_Scaffold. When the New_Scaffold's index.html.erb is displayed, the wrapper of the application.html.erb (where the yield method is called) is not displayed.
I thought it was automatic in rails, is it not ? How can I display the application.html.erb to wrap the New_Scaffold's index.html ?
The scaffold command generates a bunch of things for a model and controller for you automatically.
If you want to try Rails out try to create a new project like this:
rails new blog
cd blog
rails generate scaffold Post title body:text
rake db:migrate
rails server
This will create a basic project with one model, Post, that will have a generic setup with views and a controller that responds to all the RESTful actions.
the application.html.erb file can be found under views/layouts
I created a controller and a model. The controller is called "Admin" and the model is called "Album". I edited database.yml with proper info and did the rake db:migrate command which didn't return any errors and did migrate the db inside schema.rb. Inside the controller I wrote:
class AdminController < ApplicationController
scaffold :album
end
Next I started my server and went to http://localhost:3000/admin but instead of seeing the typical CRUD page I get the following error:
app/controllers/admin_controller.rb:3
Request
Parameters:
None
Show session dump
---
flash: !map:ActionController::Flash::FlashHash
{}
Response
Headers:
{"cookie"=>[],
"Cache-Control"=>"no-cache"}
Any idea why?
That syntax for scaffolding has been deprecated for quite some time. Nowadays, rails (versions 2.x) use the following method to scaffold a resource:
script/generate scaffold Album title:string date:date ...
That generates the scaffolding views (in app/views), the controller (app/controllers), standard tests (in test/) and, crucially, the required routes to make scaffolding work.
I believe the rails dev team took away the old syntax ("scaffold :resource") because no real application would ever leave a scaffold untouched, ie. you will always need some kind of customization. With the new syntax you can leave it untouched, but it is also much easier to customize.
If you really need your controller to be named admins, you can change the file config/routes.rb after generating the scaffolding. It makes no sense, though: Why should the URI to create a new album be called "/admins/new"?
If you are trying to create an admin area for an image album app, you are probably looking for namespaces (so you can have multiple different resources, controllers and views inside the "admin" namespace). To create an album resource within the admin namespace, write:
script/generate scaffold Admin/Album title:string date:date
In that case, your controller will be accessible as http://host/admin/albums.
Hm,
Normally you would have a controller and a model called Admin and the same thing would be about Album,
Take a look at this quick screen cast how a blog is done using scaffolding;
Creating a web-blog
the script/generate command seems not to work, someone has to provide ./script/generate , I think its a linux directory issue, you have to explicitly say you are starting from the current directory (./). hope this helps someone avoid scratching his head