得到戏剧将对象转换为字符串

我正在开发一个包含CMS webapp基本元素的webapp。 用户可以克隆存储库,运行rails服务器,并转到允许他们将应用程序从当前名称“框架”重命名为任何他们想要的页面。 经过一番争论,我决定将重命名代码放入我的控制器中(而不是在rake文件中)。 但我的问题是我的控制器无法弄清楚发生了什么。 这就是我的观点。

Rails Framework

'create' %>

这是我的控制者。

 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 

我还有一个名为renamer的模型。 这是非常基本的。 我删除了“<Base :: ActiveRecord”,因为此重命名过程中没有涉及数据库。

 class Namer end 

我的问题是,当我在表单中输入一个新名称时,rails会返回一个错误,指出:

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

我不确定为什么它急于将Namer变成一个字符串,因为我认为它只是将@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 < 'namer#create'", "") } flash[:notice] = "Enjoy your app." redirect_to(root_path) end end 

出于某种原因,当代码半成功时,它最终删除了所有的congig / environments / 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 

解决方案:这就是我的Create方法最终看起来的样子。 现在它就像我想要的那样工作。

  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 < 'namer#create'", "") } flash[:notice] = "Enjoy your app." redirect_to(root_path) end 

这一行: text.gsub("Framework", @appname)就是问题所在。 当你传递一个整个对象@appname ,gsub正在寻找一个字符串/模式作为第二个参数。 试试@appname.to_s

更新

那么你可以用param[:appname]替换gsub中的@appname并完全避免使用该类。

或者在Namer中你可以这样做:

 class Namer attr_accessor :appname end 

然后做:

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