Create A Simple User Directory Page

I want to create a new users view page that lists info from all currently registered users. I created a new view page in 'users/registrations' called "display.html.erb". I added to my 'registrations_controller': def display @users = User.all end. Then I added to my 'routes.rb' under devise_for: devise_scope :users do get '/users/:id' => 'users#display', as: 'display'. I'm getting a routing error. On my 'display.html.erb' page I have: <% @users.each do |user| %><br><%= @user.business_name %><br><% end %>. What am I doing wrong? I thought this would be the same type of set up as creating my home listings page, but the one hitch is getting the new view page to be recognized in the routes file. Yes?

Posted over 4 years ago by Amy Peterson
Posted over 4 years ago by Amy Peterson

Oh geez, okay nevermind. I got it to work by just deleting the "@user = User.find(params[:id])" from my controller file. I knew it was an easy fix : )

1
Posted over 4 years ago by Alex Yang

That should prevent the error, but keep in mind that you may not want to use the /users/:id URL format if you don't plan to do anything with the ID parameter.

1
Posted over 4 years ago by Amy Peterson

Not sure I follow...see below my routes and display.html.erb file...the only change I made after that post was in the controller file removing the @user = User.find(params[:id]) -- my controller file now just has: @users = User.all. Am I all good set up this way?

1
Posted over 4 years ago by Alex Yang

I'm not saying it won't work. My only point was that it would be silly to have a URL like www.WEBSITE.com/users/# when the # doesn't do anything. Why not just use www.WEBSITE.com/users instead?

1
Posted over 4 years ago by Amy Peterson

It's my route.rb file. I decided to try a change and moved my new display.html.erb view file into my Pages directory instead. This way I can at least see the new display page with the routes.rb saying: get '/pages/display' => 'pages#display', as: 'display'. So now I see my new page and it's empty at the moment. (If I keep the display page in the registrations directory, I can't get the page to display at all with the routes.db containing the devise_scope thing.) So here's where I am right now:

routes.rb:
get '/pages/display' => 'pages#display', as: 'display'

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

display.html.erb:
<% @users.each do |user| %>
<%= user.business_name %>
<% end %>

I know I need to define the :id in routes.rb, but when I rake routes what i see is:
display GET /users/:id(.:format) pages#display

If I change my routes file to get '/users/:id' =>..., I get a GET pages/display not defined error. Keeping it all the way it is now, I get the error: "ActiveRecord::RecordNotFound in PagesController#display" with "Couldn't find User with 'id'=".

I have 2 users in the system each with an :id, so I'm confused...?

1
Posted over 4 years ago by Alex Yang

If you have a routing error, it's either because your link's route isn't right or because your config/routing.rb file isn't set up properly. I can't say for sure without seeing a screenshot of the full error message, but I'd check that you've passed an ID parameter in your link's route...the '/users/:id' URL format needs to have an ID.

1