rails - error loading "new" page made with ruby on rails scaffolding -
i have simple rails app devise set , profile model created. have set associations , seems fine, when try access new_profile_path new user account, following error:
nomethoderror in profilescontroller#new undefined method `build' 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 in profiles controller, cant seem figure out issue. entire code 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 %> <i class="fa fa-home"></i>main <% end %> </li> <% if !current_user.profile.blank? %> <li><%= link_to profile_path(current_user.profile.id) %> <i class="fa fa-user"></i>employee profile<i class="fa fa-sort-desc"></i> <% end %> <% else %> <li><%= link_to new_profile_path %><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 %> <i class="fa fa-building-o"></i>projects<i class="fa fa-sort-desc"></i> <% end %> <ul> <li><%= link_to active_path %>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 %> <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 %>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 %> <i class="fa fa-search"></i>search <% end %> </li> <li><%= link_to reports_path %> <i class="fa fa-file-text-o"></i>reports <% end %> </li> <li><%= link_to feedback_path %> <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 , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_one :profile end i hope issue clear enough. in advance!
update:-
after updating profile controller code 1 above, receiving 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) %> <i class="fa fa-user"></i>employee profile<i class="fa fa-sort-desc"></i> <% end %> <% else %> its says there problem line:
current_user not yet have profile, can't call build on it. think want is:
@profile = current_user.build_profile this auto-generated method rails provides you. documentation little dense, here is. this might little more readable.
Comments
Post a Comment