ProfilesController#edit中的ActiveRecord :: RecordNotFound

时间:2014-04-02 03:23:04

标签: ruby-on-rails ruby ruby-on-rails-4 devise associations

我有一个应用程序,用户注册了一个电子邮件(Devise)或Omniauth工作正常。我在用户注册后添加了一种创建配置文件的方法。同样在视图中,我希望在同一页面中拥有用户信息和个人资料信息。我不确定我是否错误地执行了关联或在我的控制器中,但我收到此错误:

这是我得到的错误:

ActiveRecord::RecordNotFound in ProfilesController#edit
Couldn't find Profile with id=25
Extracted source (around line #58): @profile = Profile.find(params[:id])

这些是我正在使用的模型:

class User < ActiveRecord::Base
  after_create :create_profile
  has_one :profile, :dependent => :destroy

  private
  def create_profile
    Profile.create(user: self)
  end
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

这是Profiles控制器:

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

  def index
    @profiles = Profile.all
    @profile = current_user.profile
  end

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

  def new
    @profile = Profile.new
  end

  def edit
    @profile = current_user.profile #there was nothing before
  end

  def create
    @profile = Profile.new(profile_params)
    respond_to do |format|
      if @profile.save
        format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
        format.json { render action: 'show', status: :created, location: @profile }
      else
        format.html { render action: 'new' }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @profile = current_user.profile #there was nothing before
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
        format.json { render action: 'show', status: :ok, location: @profile }
      else
        format.html { render action: 'edit' }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @profile.destroy
    respond_to do |format|
  format.html { redirect_to profiles_url }
      format.json { head :no_content }
    end
  end

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

    def profile_params
      params.require(:profile).permit(:first_name, :last_name, :headline)
    end
end

观点:

<!-- views/profiles/_form.html.erb -->

<%= simple_form_for(@profile) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :first_name %>
    <%= f.input :last_name %>
    <%= f.input :headline %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

<!-- views/layouts/navigation.html.erb -->
<% if user_signed_in? %>
  <p class="navbar-text navbar-left">Hello <a href="#" class="navbar-link"><%= @user.name %></a>!</p>
<% end %>

<% if user_signed_in? %>
  <li><%= link_to 'Logout', destroy_user_session_path, :method=>'delete' %></li>
<% else %>
  <li><%= link_to 'Sign in with your Email', new_user_session_path %></li>
  <li><%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %></li>
  <li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
<% end %>

<% if user_signed_in? %>
  <li><%= link_to 'Users', users_path %></li>
<% end %>

<% if user_signed_in? %>
  <li class="dropdown">
    <a href="" class="dropdown-toggle" data-toggle="dropdown">Account <b class="caret"></b></a>
      <ul class="dropdown-menu">
        <li><%= link_to 'Edit profile', edit_profile_path(@user) %></li>
        <li><%= link_to 'Edit settings', edit_user_registration_path %></li>
      </ul>
  </li>
<% else %>
  <li><%= link_to 'Sign up', new_user_registration_path %></li>
<% end %>

请告诉我如果我的代码有问题或如何解决我遇到的问题。此外,如果我还应该从我的代码中放入其他内容。提前谢谢!

2 个答案:

答案 0 :(得分:1)

您可以尝试在数据库

中缺少当前用户的个人资料时处理错误
def edit
  unless current_user.profile.blank?
    @profile = current_user.profile
  else
    flash[:notice] = "No profile exists for current user"
    redirect_to root_path
  end 
end

您仍然想要当前用户的个人资料,因此您最好删除before_filter :set_profile edit行动的{{1}}

答案 1 :(得分:0)

在编辑操作

中,您似乎第一次没有用户个人资料
def edit
    @profile = current_user.profile #there was nothing before
  end

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

当前用户没有配置文件请在创建新用户记录后检入数据库。如果正在创建新的个人资料记录。

尝试更改像这样的after create方法。

def create_profile     create_profile.create!()   结束 可能存在验证错误。