Rails 4 error - rails Couldn't find User with

2019-09-14 11:01发布

问题:

I am trying to make an app in Rails 4.

I'm trying to follow CyberDude's advice in this post:

Defining Roles with Rolify

I'm getting stuck at the part where I make an index for users in the user/views file.

I am getting an error that says:

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

I can't understand this error message. Others with similar messages seem to have problems arising in other actions (like #show) where an :id is required. I think index doesn't need and id (but I'm not sure about that).

I have:

User's controller (I also have a folder in my controllers called "user" and it has controllers for regirations and omniauth callbacks):

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

I have a users index view:

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

When I try this, I get the error I posted above.

Can anyone see what I've done wrong, or decipher the error message?

回答1:

I think that your error is occuring because :index is in before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]. Simply remove :index from the list and that should take care of this error. The params hash does not contain an :id key on the index route. Check out this guide on Rails routing.

The index action is used, in this case, to grab all of the users so that they can be displayed on your index page. For example in one of my apps I have the following:

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

Patrick



回答2:

Also just remove the :index action from the bellow code:
before_action :set_user, only: [:index, :show, :edit, :update, :finish_signup, :destroy]

, actually before calling any action in your controller a private function called "def set_user" is invoked and that cause the problem in your index action. You may have to rethink about the logic you are working on.

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