2012-03-13 60 views
0

我正在研究一個包含CMS Web應用程序基本元素的web應用程序。用戶可以克隆repo,運行rails服務器,並進入一個頁面,允許他們將應用程序從當前名稱「框架」重命名爲任何他們想要的。經過一番辯論here,我決定把重命名的代碼放到我的控制器中(而不是在rake文件中)。但我的問題是,我的控制器無法弄清楚發生了什麼事。這是我的觀點。將劇本轉換爲字符串

<h1>Rails Framework</h1> 

<%= form_tag "/namer" do %> 
    <%= text_field_tag "appname" %> 
    <%= submit_tag "Name Your App" , :action => 'create' %> 

<% end %> 

而這是我的控制器。

class NamerController < ApplicationController 

    def index 
    render('new') 
    end 

    def new 
    @appname = Namer.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 rootpath 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 

我也有一個模型叫重命名。這是非常基本的。我刪除了「< Base :: ActiveRecord」,因爲在這個重命名過程中沒有涉及數據庫。

class Namer 
end 

我的問題是,當我進入邁上了一個新的名稱爲形式,軌道返回一個錯誤,指出:

TypeError in NamerController#create. Can't convert Namer into String. 

我不知道爲什麼它是如此急於轉向納默成字符串因爲我認爲它只是使用@appname變量作爲字符串。任何想法,爲什麼這是失敗?

更新:所以我對原始代碼做了一些更改,現在是它的外觀。由於某些原因,代碼確實成功運行,並對其應該的某些文件進行名稱更改。

class NamerController < ApplicationController 

    def index 
    render('new') 
    end 

    def new 
    end 

    def create 
    #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", params[:appname]) } 
    end 
    #next,change the rootpath 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") } 
    File.open(file_name, "w") { |file| file << text.gsub("post '/namer' => 
     'namer#create'", "") } 
    flash[:notice] = "Enjoy your app." 
    redirect_to(root_path) 
    end 

end 

出於某種原因,當代碼是半成功的,它結束了刪除所有congig /環境/ test.rb文件,它看起來像這樣的。

Framework::Application.configure do 

    config.cache_classes = true 
    config.serve_static_assets = true 
    config.static_cache_control = "public, max-age=3600" 
    config.whiny_nils = true 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 
    config.action_dispatch.show_exceptions = false 
    config.action_controller.allow_forgery_protection = false 
    config.action_mailer.delivery_method = :test 
    config.active_support.deprecation = :stderr 
    config.assets.allow_debugging = true 
end 

我不小心在路線文件夾中移動了其中一條線,這使得重命名代碼無法運行。 (不知道爲什麼)。所以我認爲它可能與test.rb文件清空所有文本的問題有關。這是我的routes.rb文件。

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' 


    post '/namer' => 'namer#create'  
    root :to => "namer#new" 
    match ':controller(/:action(/:id(.:format)))' 
end 

SOLUTION: 這是我的創建方法結束看起來像。現在它就像我想要的那樣工作。

def create 
    #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", params[:appname])} 
    end 
    #next,change the rootpath 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") } 

    file_name ='config/routes.rb' 
    text = File.read(file_name)  
    File.open(file_name, "w") { |file| file << text.gsub("post '/namer' => 
     'namer#create'", "") } 
    flash[:notice] = "Enjoy your app." 
    redirect_to(root_path) 
    end 

回答

0

此行:text.gsub("Framework", @appname)是問題所在。 gsub正在尋找一個字符串/模式作爲第二個參數,而您正在傳遞一個完整的對象@appname。嘗試@appname.to_s

更新

那麼你可以只用param[:appname]取代@appname在GSUB和完全避免的類。

或者在納默你可以這樣做:

class Namer 
    attr_accessor :appname 
end 

然後執行:

def create 
    @appname = Namer.new 
    @appname.appname = params[:appname] @appname.appname 
    ... 
    text.gsub("Framework", @appname.appname) 
    ... 
end 
+0

好了,一定幫助,但它並沒有解決我的問題。在brightside上,代碼能夠無誤地運行。但不是根據用戶在視圖中輸入的內容重命名文本,而是應用程序彈出:Namer:0x007f9ab5b73e08。所以問題是查看器輸入的內容沒有被轉換成變量@appname,就像我希望的那樣。有任何想法嗎? – 2012-03-13 20:27:54

+0

那麼你可以用'param [:appname]'替換gsub中的'@ appname',並完全避免這個類。或者在Namer中,你可以執行'attr_accessor:appname',然後執行: '@appname = Namer.new' '@appname.appname = params [:appname]' 然後用@appname替換@appname。 appname'。 – ScottJShea 2012-03-13 20:33:20

+0

將我的意見轉移到答案中並更好地格式化 – ScottJShea 2012-03-13 20:36:15