2013-04-10 58 views
1

我的“个人档案”控制器上有一个“未初始化的常量ProfilesController”错误。这是profiles_controller.rb:未初始化的常量ProfilesController

class ProfilesController < ApplicationController 
    def new 
    @profile = Profile.new 
    end 

    def create 
    @profile = Profile.new(params[:profile]) 

    if @profile.save 
     redirect_to profile_path, notice: I18n.t('.profile.created') 
    else 
     render action: "new" 
    end 
    end 
end 

这是routes.rb中:

resources :profiles, only: [:new, :create] 

耙路线,它的输出:

profiles POST /profiles(.:format)      profiles#create 
    new_profile GET /profiles/new(.:format)     profiles#new 

当我点击一个链接“ new_profile_path“我得到的错误,但对我来说一切似乎都没问题?控制器名称是复数,路线是否正常?

+0

难道您是手动创建控制器还是使用脚手架?之前手动创建控制器时,我遇到了类似的行为。 – BrMcMullin 2013-04-10 19:23:16

+0

手动,bt当它不工作也与发电机。两者都有同样的问题。 – John 2013-04-11 05:45:37

回答

1

真的很奇怪,我创建了一个带有生成器的Books控制器,将所有内容都重命名为Profiles,然后它正常工作。据我所见,路线是相同的。奇怪....

2

你很可能拼写错误的控制器文件。确认该文件是真的:`/app/controllers/profiles_controller.rb'

+0

是的,拼写正确 – John 2013-04-11 05:46:23

1

我有同样的问题,当我检查控制器的名称是'profile_controller.rb'(我已经手动创建它)。但在定义里面是“ProfilesController”。

类ProfilesController < ApplicationController的

def index 
end 

def new  
    @profile = Profile.new 
end 

def create 
end 

所以,如果你的控制器名称是否正确,以及您所添加的路线(“资源:配置文件”),那么它将按预期工作