0

我运行了一个 RSpec 测试用例来填写表单并提交它。我收到以下错误:

1) Sign Up Advertiser after adding valid information should create a user
   Failure/Error: expect { click_button submit }.to change(User, :user_key)
   NoMethodError:
     undefined method `model_name' for Fixnum:Class
   # /mnt/hgfs/Projekte/adserve.example.de/app/controllers/advertisers_controller.rb:31:in `tryToCreateUser'
   # /mnt/hgfs/Projekte/adserve.example.de/app/controllers/advertisers_controller.rb:14:in `create'
   # ./sign_up_advertiser_spec.rb:32:in `block (4 levels) in <top (required)>'
   # ./sign_up_advertiser_spec.rb:32:in `block (3 levels) in <top (required)>'

这是控制器的代码:

class AdvertisersController < ApplicationController

 ...

  def home
    @menuindex = 0
  end

  def create
    @user = Advertiser.new (params[:advertiser])
    tryToCreateUser
  end

  def tryToCreateUser
    if @user.save
      @user = Advertiser.retrieve(@user.id)

      redirect_to home, :notice => "You successfully signed up " + @user.full_name
    else
      render :action => "/users/new", :layout => 'application'
    end
  end
end

这就是 routes.rb 的样子

match "signup_advertiser" => "advertisers#new", :as => "signup_advertiser"
match "signup_publisher" => "publishers#new", :as => "signup_publisher"

get "advertisers_home" => "advertisers#home"

resources :advertisers

所以我猜错误是在redirect_to 部分。但我无法弄清楚。我摆弄着在“家”和其他一些东西中渲染自定义动作。我认为这是非常基本的东西,因此非常感谢您的帮助。谢谢。

4

1 回答 1

0

是的,问题就在那里。您应该在 redirect_to 方法上使用 home 作为符号:

def tryToCreateUser
  if @user.save
    @user = Advertiser.retrieve(@user.id)
    redirect_to :home, :notice => "You successfully signed up " + @user.full_name
  else
    render :action => "/users/new", :layout => 'application'
  end
end

您现在要做的是:redirect_to 0因为您实际上是在调用控制器方法“home”。

于 2013-05-14T18:39:01.047 回答