I used generate scaffold to setup the basic RESTful actions however I want to extend the actions to include something like 'purchase'. Is there a way to use the command line to generate the boilerplate (stub functions in controller file and updated route file?)?
As far as I can tell generate controller either wipes or leaves the existing file - there's no nice way to merge them.
Not by default. However, realize that in Rails 3, customizing generators isn't terribly difficult. See Creating and Customizing Rails Generators & Templates, and Bates' screencast on Generators in Rails 3.
Regarding your second "question," that's right -- the file is either replaced or overwritten.
Related
I have notice that there are many files created by generators (not only scaffold, but rails generate in general) that I don't use.
So I came up with the following questions (about best practices):
Do you use generators or it is preferable to create by hand only files you need?
If you use generators, do you keep all generated files?
You can use generators specifically to help you build individual files. For instance, you could use
rails g model Video title:string link:string
to create a model file with those as attributes, but you don't need to use scaffolding to generate everything (if that's what you're referring to)
Personally I like to use a combination -- it really depends on the project. But I'd tell you not to have extra files that you aren't using, so definitely delete the ones you aren't using (or don't plan to use).
Hope this helps.
i do use the generators and just delete the files. It is probably because I am lazy but I find this easier.
Especially with models, because it includes the migration.
Honestly it all comes down to just preference. Just don't scaffold...
There are these railscasts.
http://railscasts.com/episodes/218-making-generators-in-rails-3 With this one you find out how
to create a stylesheets and scaffold generator.
http://railscasts.com/episodes/216-generators-in-rails-3 With this one you find out how to add some files to modify the scaffolding views.
I want to do a mix of the two. I would like to create a generator that also creates scaffolding views. Kinda like Ryan Bates nifty generators or web_app_theme gem (https://github.com/pilu/web-app-theme). I have been searching for a tutorial or some information to point me in the right direction but I can't find exactly what I'm looking for.
I know I'm close. I already know how to create a generator with Railcast 218 but now, how can I make it create view files too?
I would like to run a command like this...
rails g my_scaffold_generator Post title:string body:text
This may well be too late to help, but as I found this while Googling for the same info...
It seems to me that the best approach, at least for learning the ropes, is to duplicate and then alter the existing scaffold generator.
So the first thing that tripped me up is finding the default templates, which do not live in your rails-3.2.0 directory (or whatever version you are on), but in railties-3.2.0. So for my RVM-based installation they were at:
/Users/leo/.rvm/gems/ruby-1.9.3-p194#gemset/gems/railties-3.2.0/lib/rails/generators/
[Note: your gems directory could be somewhere else entirely, use $> gem environment to find your gem paths]
In here is erb/scaffold/templates/ which has the files you'd expect (new.html.erb, _form.html.erb etc).
You can copy these files to your app's root, into lib/templates/erb/scaffold/ and they will be used instead of the default ones.
If you want to use these in a custom generator, there are two approaches:
1) Use hook_for to call the regular ERB scaffold generator from your generator.
2) Move/process the templates inside your own custom generator, using copy_file and similar methods in Thor to move them into place.
There is a decent Rails Guide on this, although I found I didn't really get it until I started digging around in .../railties-3.2.0/lib/rails/generators/... and looking at how the defaults are structured.
I am developing Rails plugin (it is 3.1 Engine) called Carrier (https://github.com/stanislaw/carrier).
In one of my rails app I want to extend Carrier's controller with some new methods - fx. add new action #comment_form to Carrier::MessagesController (I want this action only exist in my app - I don't want to add it in Engine - because it is very specific).
Two strategies I see here:
1) I copy {Carrier's plugin root}/app/controllers/carrier/messages_controller.rb file to app/controllers/carrier/ folder of my app, and then extend it (all plugin's original actions are copied to rails app controllers folder too!).
2) More accurate way I want - is just to create {My rails app}/app/controllers/carrier/messages_controller.rb and write only #comment_form method I want Carrier to be extended with.
Expecting that two controllers's content (original from plugin's folder + custom in my rails app having only new #comment_form) will superpose, I tried the second way. But Rails then stopped recognizing all original Carrier's actions (#index, #show, etc...) written in messages_controller.rb from the Carrier plugin's folder and began treating rails app's messages_controller.rb version as the only one (all original actions began treated as empty and thus began rendered through rails conventions default flow).
So my question in general is:
How to add new actions to Rails Engines controllers without copying them entirely to Rails app/controllers folder?
UPD
For now I see two solutions which allow extension of engine's controllers without serious hacks (like this gem does: https://github.com/asee/mixable_engines from this thread: Extending controllers of a Rails 3 Engine in the main app)
1) load YourEngine::Engine.config.root + 'app' + 'controllers' + 'your_controller'
inside your_controller.rb that is in #{main_app}/app/controller/your_engine folder. Notice load instead of require.
2) Devise way (according to some SO topics suggest):
In main app create new controller that subclasses engine's one + edit routes to they point to this new controller.
I am still sure some even better solutions exist. Please correct me if they do!
Your option 2) is fine because it will let you upgrade the gem seamlessly.
Your current way simply overrides the existing controller.
Say you want to extend FooController.
Create a file named foo_controller_decorator.rb in your initializer folder
In the file:
FooController.class_eval do
#your additionnal code here.
end
I know this is a very old question but in case someone else finds this question, here is a gem that does decorators nicely. It hooks into Rails ActiveSupport and adds a convention to doing decorators that is safe from circular dependencies. We have been using it in production on multiple apps for a while.
https://github.com/EPI-USE-Labs/activesupport-decorators
I'm a happy user of RoR but have one complaint. When I do script/generate scaffold it automatically generates all my files and places them in their proper folders. However, all the different scaffolds I've created name their view files the same.
I have a bunch of index.html.erb view files and when I have them open in my text editor, it's almost impossible to tell what controller they're related to.
I'd like to change the default naming scheme of the scaffold command to name the individual files to contain their view folder name. So, instead of index.html.erb, use index.home.html.
Is there a way to do this, or am I stuck? What solutions to the multiple files with the same name problem have you Rails developers discovered?
Thanks!
You're going to be fighting the Rails' conventions by going down that path and Rails works best when you work with it rather than against it. A core part of the philosophy of Rails is that there are a set of conventions that once learned make it easy to find your way around any Rails application.
Instead of trying to redefine how Rails works, I would recommend taking advantage of the features offered by your text editor or IDE for quickly navigating to the correct view template. For example, the Rails bundle within TextMate on the Mac lets you quickly open the view file associated with a particular model and there's a plugin for Vim that provides an equivalent feature.
What changes do you need to make to a Rails project to configure blueprintcss as the default stylesheet to be used when you generate scaffolding instead of scaffold.css?
I'd recommend writing your own generator, but if you want to alter the default you can:
1 - For a single app: Freeze rails and change the stylesheet the scaffold generator uses.
railsapp/vendor/rails/railties/lib/rails_generators/generators/components/scaffold/templates/style.css
2 - For all apps: Change the same style.css file in your systems rails installation.
Substitute your own scaffolding generation code. Instructions are here (with the caveat that they may be out of date).
An easier alternative may be to write a Rake action to do textual substitution in the (normally) generated source.
Look into Rails Templates.
You can write one to do much more than replace the css in a rails app. YOu can make it install gems, freeze rails, all kinds of things. Take a look at http://youvegotrails.com for an idea of what you can do.