Create a scaffold with custom namespace and existing model - ruby-on-rails

I want to generate scaffold with a custom namespace but I want it to use an existing model/table.
If I generate a scaffold like this it generate scaffold with a custom namespace but use a new table admin_locations :
rails generate scaffold Admin::Location
Is there a way to do the same thing but using the existing Location model and locations table ?

As suggested by #engineersmnky the best way to do this is :
rails g controller Admin::Locations new create edit index destroy
Then I suggest to create a blank rails project and do :
rails g scaffold Admin::Location
After this you can replace views by the ones generated on the newly created app (/app/views/admin/locations)
Maybe there is a better solution, I would love to know if it's the case !

Related

Rails 5.1 generate resource is not creating default REST actions

Very simple:
rails new myapp
cd myapp
rails generate resource Books title:string
Now look at app/controllers/books_controllers.rb and I find a two line class without any actions. From my reading of the doc, it seems to be expected to generate REST actions.
It seems the rails generate resource does not create any methods or views.
Try running
rails generate resource -h
You will see this
Unlike the scaffold generator, the resource generator does not create
views or add any methods to the generated controller.
What you need is a scaffold generator
rails generate scaffold Book title:string
I believe the correct syntax that you must use is scaffold instead of resource this will create the controller, model and migration. To sum it up the code you must use is rails generate scaffold Book title:string
Pass an extra parameter --skip-template-engine if you do not want the views to be generated.

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.

Can I have a rails resource with the same name as the joosy namespace?

I'm creating a todos app to play around with Joosy, and I want to have a rails model called Todo while also naming the joosy application todo. The following two lines create a conflict on app/controllers/todos_controller.rb no matter which order I enter them:
rails g todos title:string
rails g joosy:preloader todos
As a workaround, I've used items for my rails resource, but is there a way to use the same name?
The problem here is that by default current generator tries to make serving controller by name of an application. If you really want to use todos as Joosy app name, do the following:
Generate preloader
Manually rename controller that it generates to anything
Patch routes to reflect name modification
Now generate scaffolds
But I recommend you to rename the application to a thing like "front". It's going to be the only Joosy app among the Rails application so the name does not really matter.

Wrapping with application.html - 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

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.

Resources