Linking to a page that "changes" dependent on what user name is chosen (Error: Couldn't find User with 'id'=)

EDIT: SOLUTION. I would like to sincerely thank everyone's help for my thread! I'm very surprised with the helpful responses and have realised the main error that I have done was creating the controller by itself, then creating the html pages, whereas I should've done what meager suggested, just do RESTFUL routes.

I have done so using rails generate controller Users index show

And then modified the routes and everything else as required. So now, when I click the specific user, it goes to their specific profile!

Much thanks for everyone's help here.


I apologize for the confusing title, as I'm unsure how else to word it.

Basically, I have a bunch of different users, associated with their user name. Now, I have a page that lists all these users and when someone clicks on the user name, they will be directed to that user's profile.

My issue is directing the user to this page.

Code:

class InformationController < ApplicationController
  def all_user_page
    @users = User.all
  end

  def user_information
    @users = User.all
    @users = User.find(params[:id])
  end
end

For the all_user_page.html.erb page:

<table>
    <thead>
        <tr>
            <th> User Name </th>
            <th> Profile Summary </th>
        </tr>
    </thead>
    <tbody>
        <% @users.each do |user| %>
            <tr>
              <td> <%= link_to "#{user.user_name}", user_information_path(user), class: 'link_to' %> </td>
              <td> <%= user.description %> </td>
            </tr>
        <% end %>
    </tbody>
</table>

When I hover over the user name link, it is:

http://localhost:3000/all_user_page/user_information/10

Which is what I want, as I expect this user to have the id to be 10, as shown above. When I click this link, I then get the error:

 ActiveRecord::RecordNotFound
 Couldn't find User with 'id'=

What should I be doing now to get this working? I'm guessing something other than the id would be needed for this page to load?

EDIT: Routes

 get 'user_profiles/all_user_page' => 'user_profiles#all_user_page', :as => :all_user_page
 get 'user_profiles/user_information/:id' => 'user_profiles#user_information', :as => :user_information

Posted Dec 6, 2018, 12:49 AM UTC · edited

95
Loading comments…