rails route - undefined方法

时间:2012-04-17 05:36:44

标签: ruby-on-rails routes

我有一个名为“新手”的模特。 routes.rb文件如下所示:

  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这样的辅助方法,但为什么它会说未定义的方法?

由于

1 个答案:

答案 0 :(得分: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)