有一个戏剧将一个对象转换为一个字符串 [英] Got drama converting an obect into a string

查看:186
本文介绍了有一个戏剧将一个对象转换为一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含CMS web应用程序的基本元素的webapp。用户可以克隆repo,运行rails服务器,并被带到一个页面,允许他们将应用程序从当前名称框架重命名为任何他们想要的。在这里的一段辩论后,我决定将重命名代码放入我的控制器(而不是在rake文件中)。但我的问题是,我的控制器有麻烦弄清楚发生了什么。这是我的视图。

 < h1> Rails框架< / h1> 

<%= form_tag/ namerdo%>
<%= text_field_tagappname%>
<%= submit_tag命名您的应用程序,:action => 'create'%>

<%end%>

这是我的控制器。

  class NamerController< ApplicationController 

def index
render('new')
end

def new
@appname = Namer.new
end

def create
@appname = Namer.new(params [:appname])
#首先,将术语框架的任何实例更改为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 |文件<< text.gsub(Framework,@appname)}
end
#下一步,更改rootpath远离namer#new
file_name ='config / routes.rb'
text = File.read(file_name)
File.open(file_name,w){| file |文件<< text.gsub(namer#new,pages#home)}
flash [:notice] =享受您的应用程式。
render('pages / home')
end

end

我也有一个叫做renamer的模型。这是非常基本的。我删除了< Base :: ActiveRecord,因为在这个重命名过程中没有数据库。

  class Namer 
end

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

 在NamerController#create中的TypeError。无法将Namer转换为String。 

我不知道为什么这么急切地将Namer变成字符串,因为我认为它只是使用@appname变量作为字符串。关于为什么这是失败的任何想法?



更新:所以我对原始代码进行了一些更改,现在看起来如何。由于某种原因代码成功运行,并对其应该的一些文件做名称更改。

  class NamerController< ApplicationController 

def index
render('new')
end

def new
end

创建
#first,将术语框架的任何实例更改为应用程序的新名称
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 |文件<< text.gsub(Framework,params [:appname])}
end
#下一步,更改rootpath远离namer#new
file_name ='config / routes.rb'
text = File.read(file_name)
File.open(file_name,w){| file |文件<< text.gsub(namer#new,pages#home)}
File.open(file_name,w){| file |文件<< text.gsub(post'/ namer'=>
'namer#create',)}
flash [:notice] =享受您的应用程式。
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

我不小心移动了在routes文件夹中的一行,它以某种方式保持重命名代码不运行。 (不知为什么)。所以我想这可能是相关的问题的test.rb文件清空所有的文本。这是我的routes.rb文件。

  Framework :: Application.routes.draw do 


资源:users
资源:sessions,:only => [:new,:create,:destroy]

匹配'/ 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, framework到应用程序的新名称
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 |文件<< text.gsub(Framework,params [:appname])}
end
#下一步,更改rootpath远离namer#new
file_name ='config / routes.rb'
text = File.read(file_name)
File.open(file_name,w){| file |文件<< text.gsub(namer#new,pages#home)}

file_name ='config / routes.rb'
text = File.read(file_name)
File.open(file_name,w){| file |文件<< text.gsub(post'/ namer'=>
'namer#create',)}
flash [:notice] =享受您的应用程式。
redirect_to(root_path)
end


解决方案

p>这行: text.gsub(Framework,@appname)是问题所在。 gsub正在寻找一个字符串/模式作为第二个参数,而你传递一个整个对象, @appname 。尝试 @ 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


I am working on a webapp that contains the basic elements of a CMS webapp. Users can clone the repo, run the rails server, and be taken to a page that allows them to rename the app from the current name, "framework", to whatever they want. After a bit of debate here, I decided to put the renaming code into my controller (as opposed to in a rake file). But my problem is that my controller has trouble figuring out what's going on. This is what my view looks like.

<h1>Rails Framework</h1>

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

<% end %>

And this is my controller.

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

I also have a model called renamer. It's very basic. I removed the "< Base::ActiveRecord" because there's no database involved in this renaming process.

class Namer 
end

My problem is that when I enter a a new name into the form, rails returns an error that says:

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

I am not sure why it is so eager to turn Namer into a string since I thought it was only using the @appname variable as a string. Any ideas on why this is failing?

UPDATE: So I've made some changes to the original code and here's how it looks now. For some reason the code did successfully run and do the name change on some of the files it's supposed to.

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

For some reason when the code was semi-successful, it ended up deleting all of the congig/environments/test.rb file, which looks like this.

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

I had accidentally moved around one of the lines in the routes folder, which somehow kept the renaming code from running. (No idea why). So I figure it may be related to the problem of having the test.rb file emptied of all text. Here is 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'


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

SOLUTION: This is what my Create method ended up looking like. And now it works just like I wanted it to.

  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 

解决方案

This line: text.gsub("Framework", @appname) is where the issue is. gsub is looking for a string/pattern as the second argument while you are passing it a whole object, @appname. Try @appname.to_s.

Update

Well you could just replace @appname in the gsub with param[:appname] and avoid the class entirely.

Or in Namer you could do:

class Namer
    attr_accessor :appname
end

and then do:

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

这篇关于有一个戏剧将一个对象转换为一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆