【问题标题】:Rails 4 error - rails Couldn't find User with 'id'=indexRails 4 错误 - rails 找不到 'id'=index 的用户
【发布时间】:2016-06-01 19:58:14
【问题描述】:

我正在尝试在 Rails 4 中制作应用程序。

我正在尝试听从 Cyber​​Dude 在这篇文章中的建议:

Defining Roles with Rolify

我在用户/视图文件中为用户创建索引的部分卡住了。

我收到一条错误消息:

ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with 'id'=index

Extracted source (around line #69):
67
68
69
70
71
72

  private
    def set_user
      @user = User.find(params[:id])
    end

    def user_params

我无法理解此错误消息。其他具有类似消息的人似乎在需要 :id 的其他操作(如#show)中出现问题。我认为 index 不需要和 id (但我不确定)。

我有:

用户的控制器(我的控制器中还有一个名为“user”的文件夹,它有用于注册和omniauth回调的控制器):

class UsersController < ApplicationController

before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]

  def index
    # if params[:approved] == "false"
    #   @users = User.find_all_by_approved(false)
    # else
      @users = User.all
    # end

  end

  # GET /users/:id.:format
  def show
    # authorize! :read, @user
  end

  # GET /users/:id/edit
  def edit
    # authorize! :update, @user
  end

  # PATCH/PUT /users/:id.:format
  def update
    # authorize! :update, @user
    respond_to do |format|
      if @user.update(user_params)
        sign_in(@user == current_user ? @user : current_user, :bypass => true)
        format.html { redirect_to @user, notice: 'Your profile was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # GET/PATCH /users/:id/finish_signup
  def finish_signup
    # authorize! :update, @user


    if request.patch? && params[:user] #&& params[:user][:email]
      if @user.update(user_params)
        @user.skip_reconfirmation!
        sign_in(@user, :bypass => true)
        redirect_to root_path, notice: 'Your profile was successfully updated.'
        # redirect_to [@user, @user.profile || @user.build_profile]
        # sign_in_and_redirect(@user, :bypass => true)
      else
        @show_errors = true
      end
    end
  end

  # DELETE /users/:id.:format
  def destroy
    # authorize! :delete, @user
    @user.destroy
    respond_to do |format|
      format.html { redirect_to root_url }
      format.json { head :no_content }
    end
  end

  private
    def set_user
      @user = User.find(params[:id])
    end

    def user_params
      # params.require(:user).permit(policy(@user).permitted_attributes)
      accessible = [ :first_name, :last_name, :email, :avatar, {role_ids: []} ] # extend with your own params
      accessible << [ :password, :password_confirmation ] unless params[:user][:password].blank?
      # accessible << [:approved] if user.admin
      params.require(:user).permit(accessible)
    end

end

我有一个用户索引视图:

<div class="col-xs-10 col-xs-offset-1" style="margin-left:10%; margin-right:10%">
    <% Users.each do |user| %>
      <div class="row">
            <div class="col-xs-5">
                <%= link_to "#{user.full_name}", user %>
            </div>
            <div class="col-xs-5">
                <%= link_to "#{user.email}", user %>
            </div>
            <div class="col-xs-2">
                 <%= link_to "edit", edit_user_path(user) %>
            </div>
    </div> 
    <% end %>   

当我尝试这个时,我收到了上面发布的错误。

谁能看到我做错了什么,或者破译错误信息?

【问题讨论】:

  • 您似乎正在调用 show 操作来查找 user id = "index" 的位置。这意味着您没有调用 index 操作。
  • 从这里:before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy] 为索引操作执行 set_user 没有意义。索引列出所有用户。取出:索引。没错,索引操作不需要 id。

标签: ruby-on-rails path routes


【解决方案1】:

也只需从下面的代码中删除 :index 操作:
before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]

,实际上在调用控制器中的任何操作之前,调用了一个名为“def set_user”的私有函数,这会导致索引操作出现问题。您可能必须重新考虑您正在处理的逻辑。

    def set_user
      @user = User.find(params[:id])
    end

【讨论】:

  • @user2860931 :感谢 UP then => DOWN,希望您的问题得到解决 :)
  • 不是故意的 - 只是我在解决这个问题时遵循了另一个答案。答案是一样的,谢谢
【解决方案2】:

我认为您的错误正在发生,因为:indexbefore_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy] 中。只需从列表中删除 :index 即可解决此错误。 params 哈希在 index 路由上不包含 :id 键。在 Rails 路由上查看 this guide

在这种情况下,index 操作用于抓取所有用户,以便他们可以显示在您的索引页面上。例如,在我的一个应用程序中,我有以下内容:

class UsersController < ApplicationController

  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  def index
    @users = User.all
  end

  # omitted code

  def set_user
    @user = User.find(params[:id])
  end

end

帕特里克

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多