Reputation: 2665
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.
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
Upvotes: 0
Views: 474
Reputation: 40277
OK, Second part of your question --- is this the right place for your code that renames your App?
No, I wouldn't put it there. I would put this as a rake task (lib/tasks/namer.rake), where you would:
rake namer:rename APP_NAME=NewAppName
This would execute the rename. That's where I'd have this code.
Upvotes: 2
Reputation: 6344
Your routes file doesn't have a "post" match for namer, looks like. Easiest way to rectify that would be to put this line in somewhere.
post '/namer' => 'namer#create'
The form_tag
helper defaults to creating a post
-method form, and if you're creating a resource, that's what you'd want — you just need to make sure your route is there for it. It's possible that Rails doesn't send POST actions through the catch-all route at the bottom, but it's much better practice to ensure your routes are named somehow.
Personally, I prefer resourceful routes whenever possible. Read up on them here; I promise it's worth it.
(If this doesn't help… can you check to see if the code in your def create
function is actually firing? Drop in a debugger line or a puts
statement to find out.)
Upvotes: 0