CMS Webアプリケーションの基本要素を含むWebアプリケーションを作成しています。ユーザーはレポをクローンし、レールサーバーを実行し、現在の名前である「フレームワーク」から、アプリケーションの名前を変更してページにすることができます。少しの議論の末、hereの後に、(私はレーキファイルではなく)私のコントローラに名前変更コードを入れることにしました。しかし、私の問題は、私のコントローラーが何が起こっているのか把握することが難しいことです。これは私の見方のようです。obectを文字列に変換するドラマを取得しました
<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
私はrenamerというモデルもあります。それはとても基本的です。この名前変更プロセスに関連するデータベースがないため、「< Base :: ActiveRecord」を削除しました。
class Namer
end
私の問題は、私は、フォームにAA新しい名前を入力すると、レールが言うエラーを返すということです。
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
私が誤って何とか実行しているから名前を変更するコードを保持ルートフォルダ内の行の1の周りに移動しました。 (理由は分かりません)。だから私はそれが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
まあ、。明るい場所では、コードはエラーなく実行できました。しかし、ユーザーがビューに入力した内容に応じてテキストの名前を変更する代わりに、このアプリケーションでは、Namer:0x007f9ab5b73e08が表示されます。だから問題は、ビューアで入力されているものが、@appnameという変数に変換されていないことです。何か案は? –
さて、gsubの '@ appname'を' param [:appname] 'に置き換え、クラスを完全に避けることができます。または、ネーマーで 'attr_accessor:appname'を実行してからdo: ' @appname = Namer.new' '@appname.appname = params [:appname]' '@ appname'を' @appname 'に置き換えてください。 appname'。 – ScottJShea
私のコメントを答えに移してそれをより良い形に整えました – ScottJShea