2014-02-14 61 views
2

我周圍搜索,發現與其他問題的這個問題,但我的應用程序完全工作,這只是我的測試是拋出錯誤。奇怪的rspec錯誤`未定義的方法model_name`

_form.html.haml

= simple_form_for @employer_profile do |f| 
=f.error_notification 

.form-inputs 
    =f.input :company_name 
... 


.form-actions 
    =f.button :submit, 'Create Employer profile', id: 'employer-profile-submit' 

我甚至不知道貼什麼其他的代碼,我沒有在該應用的任何定義model_name,沒有什麼是無任何意見或在我的測試中,當我檢查東西。

完全錯誤

1) Creating Employer Profiles Employer Profile edits a profile 
Failure/Error: visit employer_profiles_edit_path(@user) 
ActionView::Template::Error: 
    undefined method `model_name' for NilClass:Class 
# ./app/views/employer_profiles/_form.html.haml:1:in  `_app_views_employer_profiles__form_html_haml__754845775744985234_44321300' 
# ./app/views/employer_profiles/edit.html.haml:4:in `_app_views_employer_profiles_edit_html_haml__3107954110108075276_44064480' 
# ./spec/features/employer_profiles_spec.rb:44:in `block (3 levels) in <top (required)>' 

employer_profile_controller.rb

class EmployerProfilesController < ApplicationController 
    before_filter :set_user 

    def new 
    @employer_profile = EmployerProfile.new(user_id: @user.id) 
    end 

    def show 
    @employer_profile = EmployerProfile.find_by_user_id(params[:user_id]) 
    end 

    def edit 
    @employer_profile = EmployerProfile.find_by_user_id(params[:user_id]) 
    end 

    def create 
    @employer_profile = EmployerProfile.new(employer_profile_params) 

    respond_to do |format| 
     if @employer_profile.save 
     format.html { redirect_to employer_profile_path(@user), notice: 'Listing was successfully created.' } 
     format.json { render action: 'show', status: :created, location: employer_profile_path(@user) } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @employer_profile.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    @employer_profile = EmployerProfile.find_by_user_id(params[:user_id]) 

    respond_to do |format| 
     if @employer_profile.update(employer_profile_params) 
     format.html { redirect_to employer_profile_path(@user), notice: 'Employer Profile was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @employer_profile.errors, status: :unprocessable_entity } 
     end 
    end 

    end 

    private 

    def set_user 
    @user = current_user 
    end 
    def employer_profile_params 
    params.require(:employer_profile).permit(:company_name, :employer_rating, :description, :website, :city, :state, :email, :address, :zip, :industry).merge!(:user_id => @user.id) 
    end 

end 

編輯:我想我可能已經固定這一怪事下降到simple_form事情。我使用了一個常規的form_for,我現在似乎沒有得到這個錯誤。

employer_profile_feature_spec.rb

require 'spec_helper' 
include Warden::Test::Helpers 
Warden.test_mode! 

    def login_user 
    before(:each) do 
     @user = FactoryGirl.create(:user) 
     login_as(@user) 
    end 
    end 


feature "Creating Employer Profiles" do 

    login_user 

    describe 'Employer Profile' do 
    describe 'create profile' do 
     it 'completes a profile and shows the results' do 
     visit '/' 
     expect{ 
      click_link('New Employer Profile') 
      fill_in 'Company name', with: 'Lost Tie LLC' 
      fill_in 'Employer rating', with: 1 
      fill_in 'Description', with: 'testing text' 
      fill_in 'City', with: 'test-city' 
      fill_in 'State', with: 'test-state' 
      fill_in 'Email', with: '[email protected]' 
      fill_in 'Website', with: 'www.example.com' 
      fill_in 'Address', with: '123 example street' 
      fill_in 'Zip', with: 12345 
      fill_in 'Industry', with: 'Oil' 
      click_button 'Create Employer profile' 
     }.to change(EmployerProfile,:count).by(1) 

     page.should have_content 'Lost Tie LLC' 
     end 
    end 

    it "edits a profile" do 
     visit employer_profiles_edit_path(@user) 
     expect(EmployerProfile.find_by_user_id(params[:user_id])).to_not be_nil 
    end 

耙路線

employer_profiles_new GET /employer_profiles/new(.:format)   employer_profiles#new 
     employer_profiles POST /employer_profiles(.:format)    employer_profiles#create 
     employer_profile GET /employer_profile/:user_id(.:format)  employer_profiles#show 
    employer_profiles_edit GET /employer_profiles/:user_id/edit(.:format) employer_profiles#edit 
         PUT /employer_profiles/:user_id(.:format)  employer_profiles#update 
+0

'simple_form_for'是鐵軌魔術。你的@ employer_profile似乎是未定義的。你的行爲是什麼樣子的? – Satya

+0

更新完整的控制器,因爲我甚至不知道.. – toolz

+0

你可以發佈失敗的測試以及? –

回答

0

@employer_profile是在你看來零 - 所以在你的編輯操作EmployerProfile.find_by_user_id(params[:user_id])將返回零。

如果你想讓它拋出一個異常時,它無法找到EmployerProfile,使用方法的爆炸變化(即find_by_user_id!

我懷疑你的編輯路線提供params[:id],而你在params[:user_id]上進行查找。檢查rake routes的輸出或檢查控制器中的參數。

+0

添加了耙路由,我不認爲有任何錯誤。我認爲這只是一個奇怪的簡單形式,可能我沒有完全遵循約定,所以它給我留下了一些怪異的東西?無論哪種方式,我只是與默認的form_for一起,並已經得到了過去似乎錯誤。 – toolz

+0

這不是一個'怪異的simple_form事物'。 '@ employer_profile'爲零。 – sevenseacat

+0

我將編輯操作從EmployerProfile.find_by_user_id(params [:user_id])更改爲使用'@ user.employer_profile'設置'@ employer_profile'。任何想法爲什麼後者工作,但不是前者? – toolz

相關問題