ruby-on-rails - ActiveRecord::RecordNotFound -- 找不到没有 ID 的用户

标签 ruby-on-rails controller

请参阅问题底部的更新...

作为引用,这个问题是根据我之前在这里遇到的一个问题所做的一些修复演变而来的:Associating Two Models in Rails (user and profile)

我正在构建一个具有用户模型和配置文件模型的应用程序。

我想将这些模型关联起来:
- 用户创建帐户后,他会自动进入“创建个人资料”页面,他创建的个人资料仅与该特定用户相关联。
- 只有拥有配置文件的用户才能对其进行编辑。

我使用 nifty_generators 生成了用户模型。当用户点击提交创建帐户时,我将他重定向到“新配置文件” View 以创建配置文件。我通过编辑用户 Controller 中的重定向路径来做到这一点。用户 Controller 如下所示:

def new
  @user = User.new
end

def create
  @user = User.new(params[:user])
  if @user.save
    session[:user_id] = @user.id
    flash[:notice] = "Thank you for signing up! You are now logged in."
    redirect_to new_user_profile_path(:user_id => @user)
  else
    render :action => 'new'
  end
end

当我单击“提交”以创建新用户时,它现在将我发送到以下网址,这似乎是正确的:localhost:3000/users/6/profile/new。但它抛出以下异常:

NoMethodError in ProfilesController#new You have a nil object when you didn't expect it! The error occurred while evaluating nil.build



跟踪表明问题出在新方法中的配置文件 Controller 中。配置文件 Controller 如下所示:
def index
  @user = User.find(params[:user_id])
  @profile = @user.profile(:order => "created_at DESC")
end

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

def new
  @user = User.find(params[:user_id])
  @profile = @user.profile.build
end

def edit
  @user = User.find(params[:user_id])
  @profile = @user.profile.find(params[:id])
end

def create
  @user = User.find(params[:user_id])
  @profile = @user.profile.build(params[:profile])
    if @profile.save
      flash[:notice] = 'Profile was successfully created.'
      redirect_to(@profile)
    else
      flash[:notice] = 'Error.  Something went wrong.'
      render :action => "new"
    end
end

此外,当我尝试查看个人资料的索引页面时,该应用程序也会引发异常(目前没有个人资料,因为我无法通过用户创建步骤来创建个人资料)。这是异常(exception):
ActiveRecord::RecordNotFound in ProfilesController#index
找不到没有 ID 的用户

这是日志告诉我的:

Processing ProfilesController#index [GET] Parameters: {"action"=>"index", "controller"=>"profiles"}

ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/profiles_controller.rb:5:in `index'



为了向您提供应用程序的其余详细信息,模型具有以下关联:
个人资料属于 _to :user
用户有 _one :profile

我在 routes.rb 文件中有这个:map.resources :users, :has_one => :profile

在抛出上面列出的第一个异常的新个人资料页面的 View 中,我有这个:
<% form_for([@user, @profile]) do |f| %>
  <%= f.error_messages %>
....
<% end %>  

在抛出上面解释的第二个异常的配置文件索引的 View 中,我有这个:
<% @profiles.each do |profile| %>
<div class="post">
    <div class="left">
        <p>Store: </p>
        <p>Category: </p>
    </div>
    <div class="right">
        <p><%=h profile.name %></p>
        <p><%=h profile.category %></p>
    </div>
    <div class="bottom">
        <p><%= link_to 'Go to profile', user_profile_path(@user, profile) %></p>
        <p><%= link_to 'Edit', edit_user_profile_path(@user, profile) %></p>
        <p><%= link_to 'Destroy', user_profile_path(@user, profile), :confirm => 'Are you sure?', :method => :delete %></p>
    </div>



作为学习练习,我花了几个小时试图自己追踪问题,但此时我不知道如何解决这个问题。感谢帮助!

更新:

jdl,根据您的要求:
个人资料/new.html.erb:
<% form_for([@user, @profile]) do |f| %>
  <%= f.error_messages %>



<div class="left">
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>required
  </p>
    <p>
    <%= f.label :category %><br />
    <%= f.text_field :category %>required
  </p>
  <p>
    <%= f.label :address1 %><br />
    <%= f.text_field :address1 %>
  </p>
  <p>
    <%= f.label :address2 %><br />
    <%= f.text_field :address2 %>
  </p>
  <p>
    <%= f.label :city %><br />
    <%= f.text_field :city %>
  </p>
  <p>
    <%= f.label :state %><br />
    <%= f.text_field :state %>
  </p>
  <p>
    <%= f.label :zip %><br />
    <%= f.text_field :zip %>required
  </p>
  <p>
    <%= f.label :phone %><br />
    <%= f.text_field :phone %>
  </p>
  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </p>

  </div>

  <div class="right">
  <p>
    <%= f.label :website %><br />
    <%= f.text_field :website %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  </div>

  <p>
    <%= f.submit 'Create' %>
  </p>
    <% end %> 

路线.rb:
ActionController::Routing::Routes.draw 做 |map|
map.signup 'signup', :controller => 'users', :action => 'new'
map.logout '注销', :controller => 'sessions', :action => 'destroy'
map.login '登录', :controller => 'sessions', :action => 'new'
map.resources : session
  map.resources :users, :has_one => :profile  

  map.root :controller => "home"   
  map.connect ':controller/:action/:id'  
  map.connect ':controller/:action/:id.:format'  
end  

配置文件 Controller (截至 2009 年 8 月 20 日,美国东部标准时间晚上 8 点)
类 ProfilesController < ApplicationController
  def index
    @users = User.all(:order => "created_at DESC")
  end

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

  def new
    @user.profile = Profile.new
  end

  def edit
    @user = User.find(params[:user_id])
    @profile = @user.profile.find(params[:id])
  end

  def create
    @user = User.find(params[:user_id])
    @profile = @user.profile.build(params[:profile])
      if @profile.save
        flash[:notice] = 'Profile was successfully created.'
        redirect_to(@profile)
      else
        flash[:notice] = 'Error.  Something went wrong.'
        render :action => "new"
      end
  end

  def update
    @profile = Profile.find(params[:id])
      if @profile.update_attributes(params[:profile])
        flash[:notice] = 'Profile was successfully updated.'
        redirect_to(@profile)
      else
        render :action => "edit"
      end
  end

  def destroy
    @profile = Profile.find(params[:id])
    @profile.destroy
      redirect_to(profiles_url)
  end
end

科迪,下面的索引页抛出以下异常:
配置文件中没有方法错误#index
显示第 14 行引发的 app/views/profiles/index.html.erb:
#的未定义方法`name'
    <div id="posts">
<% @users.each do |profile| %>
    <div class="post">
        <div class="left">
            <p>Store: </p>
            <p>Category: </p>
        </div>
        <div class="right">
            <p><%=h profile.name %></p>
            <p><%=h profile.category %></p>
        </div>
        <div class="bottom">
            <p><%= link_to 'Go to profile', user_profile_path(@user, profile) %></p>
            <p><%= link_to 'Edit', edit_user_profile_path(@user, profile) %></p>
            <p><%= link_to 'Destroy', user_profile_path(@user, profile), :confirm => 'Are you sure?', :method => :delete %></p>
        </div>
  </div> 

最佳答案

错误告诉您在这一行中:

@profile = @user.profile.build

@user.profile 为零。

由于您还没有创建配置文件,这是有道理的。相反,去做这样的事情。
@profile = Profile.new(:user_id => @user.id)

回复:索引页抛出异常。

您在 Controller 中定义 @users,然后在路径助手中引用 @user。此外,迭代 @users 应该给你 User 对象,而不是 Profile 对象。

您似乎正在尝试使用 Profile#index 操作来显示用户列表。以一种不太纯粹的 REST 方式,这还算可以。但是,我希望看到更多类似的东西。
<% @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 -%>

关于ruby-on-rails - ActiveRecord::RecordNotFound -- 找不到没有 ID 的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1303980/

相关文章:

ruby-on-rails - 如何在 Rails 中的 Web 请求中对长进程进行子线程化

html - Nokogiri 能否将无序列表视为段落?

mysql - 在 Rails ActiveRecord 中,为什么我的 JOINed 表中的列之一抛出错误消息?

ruby-on-rails - rails 中无模型的 Controller 的布线3

php - 使Laravel 8 Slug变量不区分大小写

javascript - Em.Controller 在 Ember.js 中的作用是什么

java - 如何在来自 GET 和 POST 请求的 Spring Controller 方法之间共享信息?

ruby-on-rails - 无法理解 ActionCable

Java Web 应用程序不使用注释

ruby-on-rails - Rails 的开源项目是否具有最佳实践和/或代码标准?