ActiveRecord::RecordNotFound — Couldn't find User without an ID

♀尐吖头ヾ 提交于 2019-12-02 22:23:01

The error is telling you that in this line:

@profile = @user.profile.build

@user.profile is nil.

Since you haven't created the profile yet, this makes sense. Instead, go for something like this.

@profile = Profile.new(:user_id => @user.id)

Re: The index page throwing an exception.

You are defining @users in your controller, and then referencing @user in your path helpers. Also, iterating over @users should give you User objects, not Profile objects.

It looks like you're trying to use the Profile#index action to show a list of users. That's kind-of OK, in a not-quite-pure-REST way. However, I would expect to see something more like this.

<% @users.each do |user| -%>
  <% unless user.profile.blank? -%>
    <%= h user.profile.name %>
    <%= h user.profile.category %>
    <%= link_to 'Go to profile', user_profile_path(user, user.profile) %>
  <% end -%>
<% end -%>

hmm.. I thought when User has many profiles, you can then use:

@user.profiles.build
@user.profiles.create

when User has one profile, you have to use:

@user.build_profile
@user.create_profile

Can't be sure, check API

@jdl was correct in that there is no @user.profile object in ProfilesController#new so calling @user.profile.build would throw a nil exception.

You can fix this by instantiating a new Profile object by

@user.profile = Profile.new

Later if @user gets saved then it will trigger a @user.profile and the foreign keys will be set appropriately.

Also in your ProfilesController, the #index and #show actions are kind of weird. Conventionally, the #index action returns a "list of objects" and #show is to display just one object, a specific one given an ID. However, your #index returns a very specific object because it does a User.find with a specific ID. Furthermore, since a user only has one Profile object it doesnt make sense to also load up its Profile object with an ORDER BY. There should only be one so no order is necessary. On top of that, its debatable whether you need to explicitly load the profile object as you can just access it via @user.profile and ActiveRecord will load it on demand. So your new #index looks something like

def index
  @users = User.paginate(:all, :order = "created_at desc", :page => 1, :per_page => 10)
end

This assumes you have the WillPaginate plugin, but the gist is that you are loading a LIST of objects, not just one. In your view, if you are iterating over @users and call .profile on an element of that list then ActiveRecord will load the associated Profile on the fly.

Pretty same thing for #show - no need to explicitly load the profile.

def show
  @user = User.find(params[:user_id])
end
Rekha Benada

Do one thing

In ProfileController provide session[:user_id] not params[:user_id]

def show <br>

@user = User.find(session[:user_id])<br>
@profile = @user.profile <br>
end <br>

I hope it works!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!