Rails3ルーティング道場の予習

https://github.com/hamamatsu-rb/hamamatsu-rb.github.com/wiki/%E6%B5%9C%E6%9D%BErails3%E9%81%93%E5%A0%B4-routing%E7%B7%A8
の予習

[解決済み] リダイレクトループ

  def view
    redirect_to :controller => 'catalog', :action => 'view', :id => 3
  end

だと、「このウェブページにはリダイレクト ループが含まれています」というエラーになる

  def view
    if params[:id] != "3"
      redirect_to :controller => 'catalog', :action => 'view', :id => 3
    end
  end

というようにコントローラを修正すればよい。

[未解決] ビューでidをtypoするとき、"."を入れると失敗する??

Viewで :id に "2.1" というように"."を入れると失敗する
No route matches {:action=>"view", :controller=>"catalog", :id=>"2.1"}
というエラーになる。

[未解決] rails consoleでの確認に失敗する??

rails console にて、

r.recognize_path '/products/1'

とか

r.generate :controller => 'catalog', :action => 'view', :id => '2' 

を実行すると、

RuntimeError: route set not finalized

というエラーが起きる。
よくわからない。

[未解決] rakeでWARNINGが出る

WARNING: Global access to Rake DSL methods is deprecated. Please include
... Rake::DSL into classes and modules which use the Rake DSL methods.
WARNING: DSL method RoutingDojo::Application#task called at C:/ruby/ruby-1.8.7/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/application.rb:215:in `initialize_tasks'

[解決済み] rake testが失敗する

エラーメッセージ

c:/dev/rails_app/routing-dojo/db/schema.rb doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter c:/dev/rails_app/routing-dojo/config/application.rb to limit the frameworks that will be loaded

rake db:migrate を一旦実行しておくと、成功した。(メッセージ通り)

[解決済み] catalog_controller_test.rbの元からあるテスト

get :view

は以下のエラーで失敗するようになるので、コメントアウトしておく

1) Error:
test_should_get_view(CatalogControllerTest):
ActionController::RoutingError: No route matches {:action=>"view", :controller=>"catalog"}

[未解決] rake testでfunctionalのみ実行する方法

というものはないか?

ちなみに、Vistaではすっごい時間がかかる。4分。。。
標準出力に表示される時間ログは以下。(スタートするまでに何か色々頑張っているようだ)

  • unit:
    • Finished in 12.604106 seconds.
  • functional:
    • Finished in 13.207916 seconds.

[未解決] specを使えるようにする方法が分からない

というか、調べていない

[未解決] rootディレクティブが効かない??

config/routes.rbに

root :to => "welcome#index" # root_path => '/'

を書いても、http://localhost:3000/ の表示が変わらない

[メモ] Rails3のroutesの参考ページ

http://irohiroki.com/2010/08/29/rails3-routes

[メモ] routes.rbに同じ名前のRouteがあるときの動作

後のRouteが優先される

[メモ] scaffold

rails g scaffold item name:string body:text price:integer

というコマンドを使った。
routes.rbは編集されないようだ。なので、

resources :items

を追加すると色々うれしい。

[未解決] rouging errorになってしまう

config/routes.rbに

resources :items do
  member do
    get 'short'
    post 'toggle'
  end
end

を定義して、ビューに

<%= link_to toggle_item_url(:id => "1"), toggle_item_path(:id => "1") %>

を書くと、
http://localhost:3000/items/1/toggle
へのリンクを作成できるが、Routing Error になってしまう
なぜ?
ちなみに、
http://localhost:3000/items/1/short
はRouting Errorにはならない。
getとpostの違い??

[メモ] memberとcollectionの違い

resources :items do
  member do
    get 'short'
    post 'toggle'
  end
  collection do
    get 'sold'
  end
end

paramsに id=??? を受け取る/受け取らない の違い。
ちなみに、URLは以下のようになる
http://localhost:3000/items/1/short
http://localhost:3000/items/1/toggle
http://localhost:3000/items/sold