Search code examples
ruby-on-railsrubydevisescaffolding

rails - error loading "new" page made with ruby on rails scaffolding


I have a simple rails app with devise set up and a profile model that I created. I have set up my associations and everything seems fine, however when I try to access the new_profile_path with a new user account, I get the following Error:

NoMethodError in ProfilesController#new undefined method `build' for nil:NilClass

Extracted source (around line #17): 15 16 17 18 19 20

def new @profile = current_user.profile.build respond_with(@profile) end

The problem seems to be in my profiles controller, however I cant seem to figure out the issue. This is the entire code for my profiles controller:-

class ProfilesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_profile, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
@profiles = Profile.all
respond_with(@profiles)
  end

  def show
respond_with(@profile)
  end

  def new
@profile = current_user.build_profile
respond_with(@profile)
  end

  def edit
  end

  def create
@profile = current_user.build_profile(profile_params)
@profile.save
respond_with(@profile)
  end

  def update
@profile.update(profile_params)
respond_with(@profile)
  end

  def destroy
@profile.destroy
respond_with(@profile)
  end

  private
def set_profile
  @profile = Profile.find(params[:id])
end

def profile_params
  params.require(:profile).permit(:name, :civil, :email, :level, :employment_date, :mobile, :folder, :title, :internal, :nationality, :vacation, :work_email, :experience)
end
end

My html link:-

<nav>
    <ul>
         <li><%= link_to root_path do %>
                <i class="fa fa-home"></i>Main
            <% end %>
        </li>
        <% if !current_user.profile.blank? %>
          <li><%= link_to profile_path(current_user.profile.id) do %>
             <i class="fa fa-user"></i>Employee Profile<i class="fa fa-sort-desc"></i>
          <% end %>
          <% else %>
                <li><%= link_to new_profile_path do %><i class="fa fa-user-plus"></i>Create Profile</li>
                <% end %>
          <% end %>
                <ul>
                    <li><%= link_to "Tasks / Activities", tasks_path %></li>
                    <li><%= link_to "Vacations", vacations_path %></li>
                </ul>
         </li>
         <li><%= link_to projects_path do %>
                <i class="fa fa-building-o"></i>Projects<i class="fa fa-sort-desc"></i>
                <% end %>
            <ul>
                <li><%= link_to active_path do %>Active Projects<i class="fa fa-caret-right"></i>
                    <% end %>
                    <ul>
                        <li><%= link_to "Preliminary Stage", presignature_path %></li>
                        <li><%= link_to "Design Stage", develop_path %></li>
                        <li><%= link_to "Tendered Stage", proposed_path %></li>
                    </ul>
                </li>
                <li><%= link_to "Archive", archive_path %></li>
            </ul>
         </li>
       <li><%= link_to project_docs_path do %>
        <i class="fa fa-folder-open"></i>Project Documents<i class="fa fa-sort-desc"></i>
        <% end %>
            <ul>
                <li><%= link_to "Preliminary Stage", presignature2_path %></li>
                <li><%= link_to development2_path do %>Design Stage<i class="fa fa-caret-right"></i>
                    <% end %>
                    <ul>
                        <li><%= link_to "Phase 1", phase1_path %></li>
                        <li><%= link_to "Phase 2", phase2_path %></li>
                        <li><%= link_to "Phase 3", phase3_path %></li>
                        <li><%= link_to "Phase 4", phase4_path %></li>
                    </ul>
                </li>
                <li><%= link_to "Tendered Projects", proposed2_path %></li>
            </ul>
       </li>
       <li><%= link_to search_path do %>
        <i class="fa fa-search"></i>Search
        <% end %>
       </li>
       <li><%= link_to reports_path do %>
        <i class="fa fa-file-text-o"></i>Reports
        <% end %>
       </li>
       <li><%= link_to feedback_path do %>
        <i class="fa fa-comment-o"></i>Feedback
        <% end %>
       </li>
    </ul>
</nav>

My Profile model:

class Profile < ActiveRecord::Base
	belongs_to :user
end

My User model:-

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_one :profile
end

I hope the issue is clear enough. Thanks in advance!

UPDATE:-

after updating the profile controller code to the one above, I am now receiving a new error:

ActionController::UrlGenerationError in Profiles#new

No route matches {:action=>"show", :controller=>"profiles", :id=>nil} missing required keys: [:id]

Extracted source (around line #8): 5 6 7 8 9 10 11

            <% end %>
        </li>
        <% if !current_user.profile.blank? %>
         <li><%= link_to profile_path(current_user.profile) do %>
        <i class="fa fa-user"></i>Employee Profile<i class="fa fa-sort-desc"></i>
     <% end %>
            <% else %>

its says there is a problem with this line:

  • <%= link_to profile_path(current_user.profile) do %>


  • Solution

  • current_user does not yet have a profile, so you can't call build on it. What I think you want is:

    @profile = current_user.build_profile
    

    This is an auto-generated method that rails provides for you. The documentation is a little dense, but here it is. This might be a little more readable.