I have added 'nifty-generators' to my gem file.
then executed following :
rails g nifty:layout
case -1
rails g nifty:scaffold Post title:string content:text
case -2
rails g scaffold Blog title:string content:text
rake db:migrate
When we genrate a scaffold with "nifty:" ; it does not provide "set_post" & "post_params" methods. but the later case provides those "set_post" & "post_params" methods.
Do we have any specific reason/difference between these two commands ?
https://github.com/ryanb/nifty-generators
Go to the Troubleshooting and FAQs section in the above page. !
Related
My project is on Rails 3.2 and refinerycms v 2.0.10
I just generated a new engine and and ran my bundle and rails generate commands, and my migration. Now, per the docs, I need to run db:seed but I don't want to execute a db:seed at the app level because I have several other engines and I don't want to re-seed them.
it is related to this question:
Rails engine / How to use seed?
but the answer there is to run db:seed at the app level.
So how would I say something like rake myNewEngine:db:seed ? I know it can be done but my google fu is apparently too weak to dredge it up.
You can just generate your own rake task. Create a your_engine.rake file and make sure it is loaded in your Rakefile.
namespace :your_engine do
namespace :db do
task :seed do
YourEngine::Engine.load_seed
end
end
end
Edit the YOUR_ENGINE/lib/tasks/YOUR_ENGINE_tasks.rake
namespace :db do
namespace :YOUR_ENGINE do
desc "loads all seeds in db/seeds.rb"
task :seeds => :environment do
YOUR_ENGINE::Engine.load_seed
end
namespace :seed do
Dir[Rails.root.join('YOUR_ENGINE', 'db', 'seeds', '*.rb')].each do |filename|
task_name = File.basename(filename, '.rb')
desc "Seed " task_name ", based on the file with the same name in `db/seeds/*.rb`"
task task_name.to_sym => :environment do
load(filename) if File.exist?(filename)
end
end
end
end
end
then in your main app you can execute your custom seeds commands, executing any seed file individually
$rake -T | grep YOUR_ENGINE
rake db:YOUR_ENGINE:seed:seed1 # Seed seed1, based on the file with the same name in `db/seeds/*.rb`
rake db:YOUR_ENGINE:seeds # loads all seeds in db/seeds.rb
is there a way to customize Rails 3 generator in order not to create scaffoldings.css.scss when running
rails g scaffolding MyScaffold
?
I still want myScaffold.css.scss so adding the code below to application.rb doesn't solve my issue. `
config.generators do |g|
g.stylesheets false
end`
Thanks
There is a --no-stylesheets flag you can use:
rails g scaffold MyModel --no-stylesheets
I run:
$ rails g scaffold User first_name:string last_name:string email:string website:string nodes:array links:array --orm=mongo_mapper
and get:
invoke mongo_mapper
create app/models/user.rb
(erb):1:in `template': undefined method `module_namespacing' for #<MongoMapper::Generators::ModelGenerator:0x000000035cd4a8> (NoMethodError)
...
from /home/thrive/.rvm/gems/ruby-1.9.2-p290#mlrepo/gems/mongo_mapper-0.11.1/lib/rails/generators/mongo_mapper/model/model_generator.rb:17:in `create_model_file'
...
full error output ... anyone know whats going on here?
I mean its a .rb file with what looks to be ERB code in it:
<%= module_namespacing do - %>
Error causing files:
model_generator.rb
model.rb
It looks like the edge branch called 'generator-parent-option' will do the trick:
#/Gemfile
gem 'mongo_mapper', :git => 'git://github.com/bearded/mongomapper.git', :branch => 'generator-parent-option'
I ran:
$ rails g scaffold User first_name:string last_name:string email:string website:string nodes:array links:array --skip-migration --orm=mongo_mapper
and got nice clean output:
thrive#thrive-laptop:~/rails_projects/hive$ rails g scaffold User first_name:string last_name:string email:string website:string nodes:array links:array --skip-migration --orm=mongo_mapper
invoke mongo_mapper
conflict app/models/user.rb
Overwrite /home/thrive/rails_projects/hive/app/models/user.rb? (enter "h" for help) [Ynaqdh] h
Y - yes, overwrite
n - no, do not overwrite
a - all, overwrite this and all others
q - quit, abort
d - diff, show the differences between the old and the new
h - help, show this help
Overwrite /home/thrive/rails_projects/hive/app/models/user.rb? (enter "h" for help) [Ynaqdh] Y
force app/models/user.rb
invoke test_unit
create test/unit/user_test.rb
create test/fixtures/users.yml
route resources :users
invoke scaffold_controller
identical app/controllers/users_controller.rb
invoke haml
exist app/views/users
identical app/views/users/index.html.haml
identical app/views/users/edit.html.haml
identical app/views/users/show.html.haml
identical app/views/users/new.html.haml
identical app/views/users/_form.html.haml
invoke test_unit
identical test/functional/users_controller_test.rb
invoke helper
identical app/helpers/users_helper.rb
invoke test_unit
identical test/unit/helpers/users_helper_test.rb
invoke stylesheets
identical public/stylesheets/scaffold.css
I'm In the process of migrating from Rails 2.3.11 to Rails 3.1.3 and I am now on Rails 3.0.11 and sorting out all the issues that this brings.
The first one I can't solve is: in Rails 2.3.11, I could do the following and get the required records back
#event_type_time_units = TimeUnit.find(#event.event_type.time_units)
In Rails 3.0.11, I've tried using
#event_type_time_units = TimeUnit.find_with_ids(#event.event_type.time_units)
and
#event_type_time_units = TimeUnit.find_some(#event.event_type.time_units)
The code for both of those doesn't do anything magical and I expected them not to work.
Does anyone have a pointer for me, please.
Thank you
edit: the error I get is TypeError in MeetingsController#create
Cannot visit TimeUnit
Rails 3 uses Arel aka relational algebra to fetch associations. Assuming your EventType model has an association to has_many :time_units, you can just do the following:
#event_type_time_units = #event.event_type.time_units
Furthermore, you can optimize your queries using EventType as a join model (ish):
# app/models/event.rb
belongs_to :event_type
has_many :time_units, :through => :event_type
# app/models/event_type.rb
has_many :events
has_many :time_units
Now, you can query directly, saving a SQL call:
#event_time_units = #event.time_units
In short, there's no reason to do a find on an association. The association returns an "Array" of the records. (I use "Array" in quotes, because it's not really an array, but an ActiveRecord::Association which behaves much like an array)
Aside
I highly recommend just migrating to Rails 3.1.3. It's just as difficult to migrate from Rails 2 -> 3 as 3 => 3.1. Save yourself the middle headache. In fact, given the legacy of your application, I recommend the following:
Create a new, empty Rails 3.1.3 application
Copy all your models, views, controllers, and libs to this new application
Search your existing projects for gems -> add them to your Gemfile
Review everything in config/*. This is where a LOT of changes have take place. If you have application-specific code in your existing application, port it over to the new one. Otherwise, leave it alone.
I have several controllers already set up. Now I want to start writing spec tests for them. Is there a command that generates the spec files automatically? I know rails does this for new resources, but I don't know if it does it for existing controllers/models too.
rails g rspec:controller ControllerName
When it asks you to override the existing controller, type n.
There are two options. If you want an empty spec file, you could try with:
rails g rspec:controller ControllerName
Now, if you want a spec file with initial specs for a basic REST controller, try with:
rails g rspec:scaffold ControllerName
If you've configured rspec in application.rb:
config.generators do |g|
g.test_framework :rspec
end
then rails g controller things will work. Opt not to overwrite files as they're generated.
All a spec looks like when it's generated is the following:
require 'spec_helper'
describe ThingsController do
it "should be successful" do
get :index
response.should be_successful
end
end
I often create the specs manually, as it's rather trivial.