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
Community
  • 1
  • 1
Ben Downey
  • 2,575
  • 4
  • 37
  • 57

2 Answers2

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.

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • Agreed. This screams "rake task". – Marc Talbot Mar 13 '12 at 04: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 '12 at 10:51
  • Is the rename a one time thing? Or does it happen often? – Jesse Wolgamott Mar 13 '12 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 '12 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 '12 at 13:41
  • If you're wanting this to be triggered via user interaction through a webpage, then perhaps a rake task isn't suited for it. – Ben Kreeger Mar 13 '12 at 14:19
  • OK, so I went ahead and put the renaming code back into the controller. But now I'm getting a NameType error because rails is having trouble converting Namer into a string. I'm confused. I thought I was telling Rails to just use the variable @appname in the renaming code. Why is it worried about converting Namer into a string? I've added my revised Create method to the end of the main my original post. – Ben Downey Mar 13 '12 at 16:22
  • I reposted this question to a [here](http://stackoverflow.com/questions/9690827/) since my original question has been answered. – Ben Downey Mar 13 '12 at 19:38
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.)

Ben Kreeger
  • 6,355
  • 2
  • 38
  • 53