2012-04-17 75 views
0

我有一個叫做「newbie」的模型。在routes.rb中文件看起來像:rails route - undefined method

Myapp::Application.routes.draw do 
    resources :newbies 

    root to: 'static_pages#home' 
    match '/about', to: 'static_pages#about' 

控制器是這樣的:

class NewbiesController < ApplicationController 

    def show 
     @newbie = Newbie.find(params[:id]) 
    end 
    ...... 
    end 

當我寫測試:

require 'spec_helper' 

describe "Newbie pages" do 
    subject { page } 
    describe "profile page" do 
    let(:newbie) { FactoryGirl.create(:newbie) } 
    before { visit newbie_path(newbie)} 

    it { should have_selector('h1', text: newbie.name) } 
    it { should have_selector('title', text: newbie.name) } 
    end 
end 

它總是失敗,說:

1) Newbie pages profile page 
Failure/Error: before { visit newbie_path(newbie)} 
NoMethodError: 
    undefined method `newbie_path' for #  <RSpec::Core::ExampleGroup::Nested_2::Nested_2:0x007f97f7f33068> 
# ./spec/requests/newbie_pages_spec.rb:16:in `block (3 levels) in <top (required)>' 

2) Newbie pages profile page 
Failure/Error: before { visit newbie_path(newbie)} 
NoMethodError:undefined method `newbie_path' for 

我認爲資源:新手會創建像newbie_path這樣的輔助方法,但爲什麼它說未定義的方法?

感謝

+0

ü可以發佈的所有路由。通過耙路線 – sameera207 2012-04-17 05:51:44

+0

哇,問題解決了。看起來像鐵軌認爲新手是newby而不是新手的複數newby_path作品...感謝提醒 – alexZ 2012-04-17 06:01:06

回答

0

當您運行 耙路線

新手生成路由資源是從你期望的一個完全不同的。

newbies GET /newbies(.:format) 
{:action=>"index", :controller=>"newbies"} 
     POST /newbies(.:format) 
{:action=>"create", :controller=>"newbies"} 

new_newby GET /newbies/new(.:format) 
{:action=>"new", :controller=>"newbies"} 

edit_newby GET /newbies/:id/edit(.:format) 
{:action=>"edit", :controller=>"newbies"} 

newby GET /newbies/:id(.:format) 
{:action=>"show", :controller=>"newbies"} 
PUT /newbies/:id(.:format) 
{:action=>"update", :controller=>"newbies"} 

DELETE /newbies/:id(.:format) 
{:action=>"destroy", :controller=>"newbies"} 

所以,你應該用

newby_path(newbie) 
+0

是的,剛剛發現以及..謝謝 – alexZ 2012-04-17 06:02:55

相關問題