【问题标题】:No route matches {:action=>"show", :controller=>"profiles", :user_id=>nil}, missing required keys: [:user_id]没有路由匹配 {:action=>"show", :controller=>"profiles", :user_id=>nil},缺少必需的键:[:user_id]
【发布时间】:2019-12-10 16:24:42
【问题描述】:

我的视图不知何故没有得到控制器的显示操作,并以某种方式返回 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 %>

我希望,当我点击“显示用户个人资料”时,我将被重定向到该用户的个人资料。在我的配置文件控制器中,我将显示操作定义如下:

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

...但没有任何工作。谢谢!

【问题讨论】:

  • 请出示您的config/routes.rb文件

标签: ruby-on-rails


【解决方案1】:

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

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

由于它是嵌套路由,您需要同时传递对象/ID。

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    相关资源
    最近更新 更多