2

I've searched around and found this issue with other questions, but my app works completely, it's just my tests that are throwing the error.

_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'

I don't even know what other code to paste, I've not defined model_name anywhere in the app, nothing is nil in any views or in my tests when I check for stuff.

full error

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

edit: I think I may have pinned this weirdness down to a simple_form thing. I used a regular form_for and I don't seem to be getting the error for now.

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 

rake routes

  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
4
  • simple_form_for is rails magic. Your @employer_profile seems to be undefined. What does your action look like?
    – Satya
    Feb 14, 2014 at 1:40
  • updated with full controller because I don't even know..
    – toolz
    Feb 14, 2014 at 1:49
  • Can you post the failing test as well?
    – Zach Kemp
    Feb 14, 2014 at 2:00
  • updated with the feature test
    – toolz
    Feb 14, 2014 at 2:03

1 Answer 1

0

@employer_profile is nil in your view - so in your edit action EmployerProfile.find_by_user_id(params[:user_id]) is returning nil.

If you want it to raise an exception when it can't find an EmployerProfile, use the bang variation of the method (ie. find_by_user_id!)

I suspect your edit route is providing params[:id], and you're doing a lookup on params[:user_id]. Check the output of rake routes or inspect your params in your controller.

4
  • added rake routes, I don't think there are any mistakes. I think this is just a weird simple_form thing where maybe I wasn't following conventions perfectly so it threw some weirdness at me? Either way I just went with the default form_for and have gotten past that error it seems.
    – toolz
    Feb 14, 2014 at 2:41
  • it is not a 'weird simple_form thing'. @employer_profile is nil. Feb 14, 2014 at 2:49
  • I changed my edit action from EmployerProfile.find_by_user_id(params[:user_id]) to setting @employer_profile with @user.employer_profile. Any idea why the latter works but not the former?
    – toolz
    Feb 14, 2014 at 4:26
  • well if params[:user_id] is not the same as @user.id, no guaranteeing what will happen. There isn't enough info to say Feb 14, 2014 at 5:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.