Wrapping with application.html - rails - ruby-on-rails

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

Related

Rails generate controller command not building view files

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

Why doesn't "g controller" also create view file?

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.

Custom erb generator called from scaffold

I am looking to not only customise my erb scaffold templates but also to add new templates.
In ScaffoldGenerator < Erb::Generators::Base I can see there there is a way to provide additional templates in %w(index edit show new _form).
So I have created a custom erb generator and templates directory in my application in lib/generators/erb/scaffold/.
However when I run rails g scaffold Something, my custom generator is not picked up by via scaffold_controller. Does this mean I need to provide a custom scaffold generator to use a custom scaffold_controller generator just so it can then use my erb generator?
I can also see the scaffold argument:
ScaffoldController options:
-e, [--template-engine=NAME] # Template engine to be invoked
# Default: erb
Am I able to provide my erb generator as the template engine to be used?
(Using Rails 4)
lib/templates/erb/scaffold is the correct path for rails scaffold.
Just in case...
model goes in /lib/templates/active_record/model/model.rb
controller goes in /lib/templates/rails/scaffold_controller/controller.rb

Prefix for the rails generators

Is there a way to generate a scaffold with the admin folder prefix..For example I want one Admin folder and a few controllers for that admin folder. I want to do a scaffold for each controller in the admin folder and i wanted to know if there was something like
script/generate scaffold admin:something somefield:string
The scaffold generator can take a namespaced argument:
# Rails 3
rails g scaffold Admin::Something somefield:string
# Rails 2
script/generate scaffold Admin::Something somefield:string
Note, that if you'd like to leave model unprefixed, you have to edit some of generated files by hand to implement it. That is why I created rails-admin-scaffold gem and wrote detailed article about custom admin area scaffolding. If that's already not relevant for you, maybe it'll help someone else.

Why is Scaffolding Not Working in Ruby on Rails?

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

Resources