Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the app name from inside a rails template when generating rails app

I'm messing around with rails 2.3 templates and want to be able to use the app name as a variable inside my template, so when I use...
rails appname -m path/to/template.rb
...I want to be able to access appname inside template.rb. Anyone know how to do this?
Thanks

like image 963
danengle Avatar asked Mar 11 '09 06:03

danengle


5 Answers

I was looking for an answer to this question. unfortunately the answer above (@root) doesn't seem to work in Rails 3.

Here's the variables you can access in Rails 3 app templates (even easier):

@app_name
@app_path
like image 72
Mike Fischer Avatar answered Nov 09 '22 10:11

Mike Fischer


Thanks for the answers. Mike Woodhouse, you were so close. Turns out, all you need to do to access the appname from inside your rails template is...

@root.split('/').last  

The @root variable is the first thing created when initializing templates and is available inside your rails templates. RAILS_ROOT does not work.

like image 37
danengle Avatar answered Nov 09 '22 10:11

danengle


In Rails 3, use the app_name attribute.

See the documentation for the Rails::Generators::AppGenerator.

like image 40
Daniel Kehoe Avatar answered Nov 09 '22 10:11

Daniel Kehoe


I ran into a similar problem, none of the variables listed above were available to me in Rails 4. I found that @name was available while running

rails plugin new engines/dummy -m my_template.rb

There are other useful variables available from within the template. You can see for yourself and play around by utilizing pry. Inside my template I added

require 'pry'; binding.pry

and then ran ls to show a list of available instance variables

ls -i
instance variables:
    @_initializer            @app_path  @behavior   @destination_stack  @extra_entries  @name           @output_buffer   @shell
    @_invocations            @args      @builder    @dummy_path         @gem_filter     @options        @rails_template  @source_paths
    @after_bundle_callbacks  @author    @camelized  @email              @in_group       @original_name  @shebang
like image 39
mriddle89 Avatar answered Nov 09 '22 11:11

mriddle89


There's probably a more straightforward way, but this seems to work:

RAILS_ROOT.split('/').last

EDIT: Bleah - this got voted down once, and the voter was right. If I'd read the question more carefully, I'd have noticed the 2.3 and template.rb elements. Apologies.

I suspect that RAILS_ROOT won't have been created at the point that you need the app name. Looking at ruby\lib\ruby\gems\1.8\gems\rails-2.2.2\bin\rails, however, almost the first thing that happens is this:

app_path = ARGV.first

It's used at the end of the script to allow a chdir and freeze to be done if needed - I didn't know I could insta-freeze at creation, so I learned something new at least. ARGV then gets used here:

Rails::Generator::Scripts::Generate.new.run(ARGV, :generator => 'app')

which quickly gets us to the place where ARGV is really handled:

rails-2.3.1\lib\rails_generator\scripts.rb

where I see

Rails::Generator::Base.instance(options[:generator], args, options).command(options[:command]).invoke!

Somewhere below here is probably where the templating gets handled. I'm afraid I'm at a very early stage with 2.3 and templating is an area that I haven't looked at yet.

Does that help any better than my first effort?

like image 4
Mike Woodhouse Avatar answered Nov 09 '22 12:11

Mike Woodhouse