I’ve created a basic framework for webapps (some static pages, user authentication, unit/integration testing with rspec). I’d like to use this as a foundation for future webapps, but I need to setup a way to rename it after cloning it from github. I got some help generating the renaming code here]1, but I'm struggling to figure out how to integrate it.
I originally wrote had the renaming code in a rakefile, but now I think maybe it should be in the controller. Unfortunately, I haven't been able to make my code work. I've got a view that allows the user to enter a new name for the app. The idea is that the user would clone the framework repo, cd into the framework directory, start rails server, then go to local host on their browser to rename the file from there. But the view that's suppose to enable that isn't working.
views/namer/new/html.erb
<h1>Rails Framework</h1>
<%= form_tag "/namer" do %>
<%= text_field_tag "appname" %>
<%= submit_tag "Name Your App" , :action => 'create' %>
<% end %>
I can't get the "submit" action to work to work properly. Here's what my controller looks like.
controllers/namer_controller.rb
class NamerController < ApplicationController
def index
render('new')
end
def new
end
def create
@appname = Namer.new(params[:appname])
#first, change any instances of the term "framework" to the new name of the app
file_names = ['config/environments/test.rb', 'config/environments/production.rb',
'config/environment.rb']
file_names.each do |file_name|
text = File.read(file_name)
File.open(file_name, "w") { |file| file << text.gsub("Framework", @appname) }
end
#next,change the root path away from namer#new
file_name ='config/routes.rb'
text = File.read(file_name)
File.open(file_name, "w") { |file| file << text.gsub("namer#new", "pages#home") }
flash[:notice] = "Enjoy your app."
render('pages/home')
end
end
Any idea what I'm doing wrong?
Also, it the controller really the best place for the "renaming" code?
edit: here's my routes.rb file.
Framework::Application.routes.draw do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => "namer#new"
match ':controller(/:action(/:id(.:format)))'
UPDATE: I've changed my code in a few ways.
- Routes: added post '/namer' => 'namer#create'
- Created a Renamer model. Models/renamer.rb is just "class Namer" and then "end." (I removed the "< Base::ActiveRecord" because there's no database involved.)
- Created a rake file for the renaming code. It just called "renamer.rake".
Things are looking good, but I'm still looking for a way to call the rake file from the controller. Any suggestions?
UPDATE 2: Here's my revised Create method for my controller. Now the renaming code is here instead of in a rake file.
def create
@appname = Namer.new(params[:appname])
file_names = ['config/environments/test.rb', 'config/environments/production.rb',
'config/environment.rb']
file_names.each do |file_name|
text = File.read(file_name)
File.open(file_name, "w") { |file| file << text.gsub("Framework", @appname) }
flash[:notice] = "Enjoy your app."
render('pages/home')
end