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

时间:2019-12-10 16:24:42

标签: ruby-on-rails

我的视图以某种方式无法获得控制器的show动作,并且以某种方式返回user_id nil。基本上,我有一个UsersController和一个ProfilesController,并且在Users#Index中显示所有具有名字和姓氏的用户。

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

我希望的是,当我单击“显示用户个人资料”时,我将重定向到该用户的个人资料。在我的个人资料控制器中,我定义了show动作,如下所示:

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为nil。

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

...但无济于事。谢谢!

1 个答案:

答案 0 :(得分:1)

您需要在链接中传递user_id,请改用它

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

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

希望有帮助!