没有路由匹配 {:action=> "show" , :controller=> "profiles" , :user_id=>nil}, 缺少必需的键: [:user_id]



我的视图不知何故没有得到控制器的显示操作,并以某种方式返回user_id nil。基本上我有一个用户控制器和一个配置文件控制器,我在用户#索引中显示所有具有名字和姓氏的用户。

<% @users.each do |user| %>
<table style="width:50%">
<tr>
<th><%= link_to 'Show user profile', user_profile_path(@profile) %> <%= user.first_name%>   <%= user.last_name%></th>
</tr>
</table>
<% end %>

我希望的是,当我单击"显示用户配置文件"时,我将被重定向到该用户的配置文件。在我的配置文件控制器中,我定义显示操作如下:

class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
def show
@user = User.eager_load(:profile).find(params[:user_id])
@profile = @user.profile
end
def new
@user = current_user
@profile = Profile.new
end
def edit
@user = current_user
@profile = @user.profile
end
def create
@user = current_user
@profile = @user.build_profile(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully created.' }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new, notice: 'Did not save' }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :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 users_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_profile
@profile = current_user.profile
end
def profile_params
params.fetch(:profile, {}).permit(:about, :avatar)
end
end

同样在我的用户模型中,您可以看到相关性:

class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:omniauthable, omniauth_providers: %i[facebook]

has_one :profile, dependent: :destroy
after_create :create_profile
accepts_nested_attributes_for :profile
validates :first_name, presence: true
validates :last_name, presence: true

def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0, 20]
name = auth.info.name
user.first_name = name.split(" ")[0]
user.last_name = name.split(" ")[1] 
end
end
end

如果我的想法正确,它应该将配置文件作为 @user.profile 并显示该特定用户的配置文件。但是我得到一个错误,说id为零。

Request
Parameters:
None

我也尝试了一下实验:

<%= link_to 'Show user profile', user_profile_path(@user) %>
<%= link_to 'Show user profile', user_profile_path(@user.profile) %>
<%= link_to 'Show user profile', user_profile_path(@user,@profile) %>
<%= link_to 'Show user profile', user_profile_path(@user,profile) %>

。但没有一个奏效。谢谢!

你需要在链接中传递user_id,改用这个

<%= link_to 'Show user profile', user_profile_path(user, @profile) %> 

由于它是嵌套路由,因此您需要传递两个对象/ID。

希望对您有所帮助!

最新更新