2014-04-02 37 views
0

我有一個應用程序在哪裏用戶註冊一個電子郵件(設計)或工作正常的Omniauth。我在用戶註冊後添加了一個創建配置文件的方法。同樣在視圖中,我希望在同一頁面中擁有用戶信息和個人資料信息。我不知道如果我這樣做的關聯不正確,或在我的控制器,但我得到這個錯誤:ActiveRecord :: RecordNotFound ProfilesController#編輯

這是我收到的錯誤:

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 

這是配置文件控制:

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 %> 

請讓我知道如果在我的代碼有問題或如何解決我遇到的問題。另外,如果還有別的東西,我應該把它放在我的代碼中。先謝謝你!

+0

你這是什麼完全想要編輯操作,當前用戶的配置文件還是要通過其ID來搜索配置文件? – RSB

+0

我想能夠編輯當前用戶的配置文件'params.require(:profile).permit(:first_name,:last_name,:headline) ',我擁有這樣的一切,似乎無法理解在哪裏錯誤是。 – crentist

+0

查看我的回答 – RSB

回答

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 

你無論如何要天寒當前用戶,所以你應該更好地爲edit行動刪除before_filter :set_profile

0

這似乎是在你第一次沒有用戶配置文件,以便於編輯動作

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

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

沒有配置文件爲當前用戶請在數據庫創建新的用戶記錄後。如果新的配置文件記錄正在創建或沒有。

嘗試更改像這樣的after create方法。

高清create_profile create_profile.create!() 結束 有可能是驗證錯誤。

+0

我認爲它正在創建,這是我在控制檯中得到的: 'SQL(167.1ms)INSERT INTO「profiles」(「created_at」,「updated_at」,「 user_id「)VALUES($ 1,$ 2,$ 3)RETURNING」id「[[」created_at「,Wed,02 Apr 01 4 05:22:38 UTC +00:00],[」updated_at「,2014年4月2日,星期三05:22:38 UTC +00:00],[「user_id」,26]]' '(83.3ms)COMMIT' – crentist

+0

@crentist請檢查數據庫並通知我。 –

+0

好的,我做了:'u = User.last'然後'u.profile'並得到'Profile Load(242.7ms)SELECT「profiles」。* FROM「profiles」WHERE「profiles」。「user_id」= $ 1 LIMIT 1 [[「user_id」,1]]' '=>無' – crentist

相關問題