Got drama converting an obect into a string

試著忘記壹切 提交于 2019-12-13 06:12:19

问题


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 

回答1:


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


来源:https://stackoverflow.com/questions/9690827/got-drama-converting-an-obect-into-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!