Reputation: 1209
I've stuck with a little problem with showing user data in blog I'm writing. I have profiles_controller with the following code:
class ProfilesController < ApplicationController
def index
@profiles = User.all
end
def show
@profile = User.find(params[:user_id])
end
end
following routes in routes.rb:
get '/profiles', to: "profiles#index", as: 'profiles'
get '/profiles/:user_id', to: "profiles#show", as: 'profile'
and following templates:
index.html.erb:
<div class="container">
<div class="row">
<% @profiles.each do |user| %>
<div class="col-md-6">
<p><%= user.first_name %> <%= user.last_name %></p>
<%= link_to 'View Profile', profile_path %>
</div>
<% end %>
</div>
</div>
show.html.erb
<div class="container">
<p><%= @profile.first_name %> <%= @profile.last_name %></p>
<p><%= @profile.email %></p>
<p><%= @profile.about %></p>
</div>
I use extended devise
User
model with added attrs as first name, last name and 'about' section.
When I try to navigate either to /profiles
path, it returns me following error:
ActionController::UrlGenerationError in Profiles#index
No route matches {:action=>"show", :controller=>"profiles"} missing required keys: [:user_id]
When I remove
<%= link_to 'View Profile', profile_path %>
line from index.html.erb, /profiles
path works, but /profile
path still doesn't. What have I done wrong?
Upvotes: 0
Views: 83
Reputation: 6870
In your show
action in the controller, it should be:
User.find(params[:id])
Also, the link should be:
<%= link_to 'View Profile', profile_path(user) %>
Upvotes: 1