我有一個應用程序在哪裏用戶註冊一個電子郵件(設計)或工作正常的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 %>
請讓我知道如果在我的代碼有問題或如何解決我遇到的問題。另外,如果還有別的東西,我應該把它放在我的代碼中。先謝謝你!
你這是什麼完全想要編輯操作,當前用戶的配置文件還是要通過其ID來搜索配置文件? – RSB
我想能夠編輯當前用戶的配置文件'params.require(:profile).permit(:first_name,:last_name,:headline) ',我擁有這樣的一切,似乎無法理解在哪裏錯誤是。 – crentist
查看我的回答 – RSB