2012-03-13 62 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