NoMethodError:Fixnum:Class的未定义方法`model_name'

时间:2013-05-14 18:31:34

标签: ruby-on-rails rails-routing

我运行一个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部分。但我无法弄明白。我在'家'和其他一些东西中摆弄自定义动作。我认为这是非常基本的,所以非常感谢帮助。感谢。

1 个答案:

答案 0 :(得分:0)

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

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”。