0

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.

  1. Routes: added post '/namer' => 'namer#create'
  2. 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.)
  3. 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
3
  • Can you paste in your routes.rb with the namer route section in there? Mar 13, 2012 at 1:35
  • Post your routes.rb. What's the error?
    – maletor
    Mar 13, 2012 at 1:36
  • I don't actually get any errors. My problem is that the for renaming the app code doesn't get executed.
    – Ben Downey
    Mar 13, 2012 at 2:14

2 Answers 2

2

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.

8
  • Agreed. This screams "rake task". Mar 13, 2012 at 4:00
  • OK. How do I trigger a rake to run when the user clicks the "Rename your app" button in the namer\view.html.erb?
    – Ben Downey
    Mar 13, 2012 at 10:51
  • Is the rename a one time thing? Or does it happen often? Mar 13, 2012 at 12:04
  • Renaming is a one time event. Once the user renames the app, that's it, there's no more renaming.
    – Ben Downey
    Mar 13, 2012 at 12:08
  • I'm struggling to make the code suggested above work (rake namer:rename APP_NAME=NewAppName). I'm trying to put it into my Create method like this: name:rename appname=@appname. But apparently, that's not a smart way to do it (because it's not working). What the smart way to setup this code?
    – Ben Downey
    Mar 13, 2012 at 13:41
0

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.)

1
  • Excellent. Is your create action working, then? Let me know by marking my response as the solution. Mar 13, 2012 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.