I have been tinkering around with a Rails project (actually, trying to get it to pick up view templates from lib/templates, which was successful). Now, however, if I generate a scaffold, I get the view files, the migration, but NOT the model file - it creates a zero length file in app/models, but gives it no content.
What on earth have I done?
The way in which I got the thing to pick up the templates was to add a
config.generators do |g|
block, but even if I comment that out now, I can't get rails g model ... to create a model file.
Any clues? FWIW, if I go into an existing alternative rails project, I can create model files perfectly - it's something to do with this project. And I'd quite like to find out what it is rather than blowing the project away and starting again...
Related
I am trying to follow an example which named "Finding a Cart" and here is my code
rails new n
cd n
cd bin
rails generate scaffold Cart
rails db:migrate
Seems it works very smoothly, in my book, they said that there is a file named current_cart.rb at folder n\app\controllers\concerns and I must store the ID of the cart in the session by indexing it with the :cart_id symbol, but my problem is, I can not find the file name current_cart.rb in that folder. There is only the file named .keep ?
Should I make a file named current_cart.rb and copy paste the content in that book into that file? Thank you very much for reading my problem.
Anh Bui: I noticed a few points which not looking good to me.
Why did you go inside bin(cd bin) and run scaffold that does not require, you can run the same from folder n itself.
You ran the scaffold for Cart then how you can look for file current_cart.rb.
In general, concerns file won't create by default you need to create that file inside concerns folder for Model or controller.
Create concerns as a module concern_name and include that concern in the model or controller where you wants use concern functionality
I'm trying to add a method to the DateTime class like so:
class DateTime
def ymd(sep = "/")
strftime("%Y#{sep}%m#{sep}%d")
end
end
I put this in #{config.root}/lib/datetime.rb and updated the autoload_path to include #{config.root}/lib (since that seems to go in and out of the conventional autoload path). That didn't work, so I also tried putting it in a random directory (#{config.root}/blah and added that path to the autoload_paths line in the config).
In all of the above cases, I'm only able to use the new method in the rails console if I require 'datetime' first, and I'm not able to use it in controllers or view templates no matter what I do.
So,
Should the file be called datetime.rb or date_time.rb? (I've tried both so far and neither are currently working)
Where should I be putting this file so I can use the new method in models, controllers and views?
Any idea why I can require it in the console, but it doesn't autoload there?
The app is currently running rails 3.2.21, but I'll switch to rails 4 at some point so answers for either version are appreciated.
I have myself a generator that, as part of a set of other operations, needs to set up a model class with a bunch of mixins, defaults, comments, etc.
I want to be using the same rails g model ... code (I'm calling invoke from my generator), but the problem is that there's a conflict because my template and the model generator's template are trying to splat each other:
$ be rails g entry_form karaoke events full_name:string group_name:string
create app/controllers/karaokes_controller.rb
create app/views/karaokes/show.html.erb
create app/views/karaokes/thanks.html.erb
route resource :karaoke
create app/models/karaoke_entry.rb
invoke active_record
create db/migrate/20111004004008_create_karaoke_entries.rb
conflict app/models/karaoke_entry.rb
Overwrite app/models/karaoke_entry.rb?
(enter "h" for help) [Ynaqdh]
Any recommendations how to get around this?
(The best I've come up with is to maybe move my model file creation to the bottom, and find some way to force template / copy_file to go ahead and overwrite the file without bothering the user, but I can't see any pre-existing way of doing this.)
Append force: true or skip: true to the template call:
template "model.rb", "app/models/#{model}.rb", force: true
Some group brainstorming turned up a way to work around this problem.
You can't override (in so far as I can tell), but you can tell the model generator to skip files that already exist. This works:
# Create the model definition from a template:
template "model.rb", "app/models/#{model}.rb"
# ... Later, get Rails to create everything else:
Rails::Generators.invoke("model", ["Example", "title:string", "--skip"])
(I would still welcome a way to allow template to override files.)
This question already has answers here:
How to rename rails controller and model in a project
(8 answers)
Closed 7 years ago.
Is there an easy way to rename a controller? The only way I know of is to either do it by hand or generate a new controller move the code over and destroy the old one. Seems like there has to be a programmatic way to do this.
Some IDE's (like IntelliJ's RubyMine) will let you Refactor -> Rename a file/variable/method etc, although it's not as reliable in a dynamic language like Ruby as it is in a language like Java.
I had just generated a controller and so I did not have an associated model or database table. I decided to just rename all the files and relevant content that was created when I generated the controller. It is not an 'easy' way to rename the controller but I had confidence in my knowledge of what had been created and what I needed to refactor.
There is a good guide on the ruby on rails guides websites that shows what is generated and what you need to edit or you can see what a typical controller generates below:
$ bin/rails generate controller Greetings hello
create app/controllers/greetings_controller.rb
route get "greetings/hello"
invoke erb
create app/views/greetings
create app/views/greetings/hello.html.erb
invoke test_unit
create test/controllers/greetings_controller_test.rb
invoke helper
create app/helpers/greetings_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/greetings.js.coffee
invoke scss
create app/assets/stylesheets/greetings.css.scss
Also, don't forget to edit the contents of the files above, things like descriptions in your assets files, controller class names and module names etc.
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