I created a user profile page that shows all the listings the user made. Each listing has a 'show' link that links to a new page that shows the listing individually on a different page. I got this to work for the user profile page. I'm using this link
<li><%= link_to "Show", user_listing_path(name: @user.name, id: listing.id) %></li>
However, now I want to create a listings index page that shows all listings of every user. Each listing again should have a 'show' link that links to a page that shows the listing individually. The same link that worked on the user profile page does not work on the listing index page.
I get the following error undefined method `name' for nil:NilClass and it points to
Does anyone know why?
User show file (show.html.erb)
<div class= "showuser">
<div class="error-message">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
</div>
<h4>
<%= gravatar_for @user %>
<%= @user.name %>
</h4>
<div class="span 8">
<% if @user.listings.any? %>
<h3> Job Posts (<%= @user.listings.count %>)</h3>
<ol class="listings">
<%= render @listings %>
<% @listings.each do |listing| %>
<% end %>
</ol>
<%= will_paginate @listings %>
<% end %>
</div>
</div>
listing file (_listing.html.erb)
<li>
<h4><%= listing.title %></h4>
<p><%= listing.location %></h4><br>
<span class="content"><%= listing.description %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(listing.created_at) %> ago
</span>
<li><%= link_to "Show", user_listing_path(name: @user.name, id: listing.id) %></li>
<% if current_user?(listing.user) %>
<li><%= link_to "Edit", edit_listing_path %></li>
<%= link_to "delete", listing, method: :delete,
data: { confirm: "You sure?" },
title: listing.description %>
<% end %>
</li>
listing controller
class ListingsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy, :edit, :update]
before_action :correct_user, only: [:destroy, :edit, :update]
def create
@listing = current_user.listings.build(listing_params)
if @listing.save
flash[:success] = "Job Post created"
redirect_to current_user
else
render 'listings/new'
end
end
def edit
end
def update
if @listing.update_attributes(listing_params)
flash[:success] = "Listing updated"
redirect_to @listing
else
render 'edit'
end
end
def show
@user = User.find_by_name(params[:name])
@listing = Listing.find_by_id(params[:id])
end
def new
@listing = Listing.new
@listings = Listing.paginate(page: params[:page])
end
def destroy
@listing.destroy
redirect_to current_user
end
def index
@listings = Listing.all
@listings = Listing.paginate(page: params[:page])
@user = User.find_by_name(params[:name])
@listing = Listing.find_by_id(params[:id])
end
private
def listing_params
params.require(:listing).permit(:description, :location, :title)
end
def correct_user
@listing = current_user.listings.find_by(id: params[:id])
redirect_to current_user if @listing.nil?
end
end
listing show file (show.html.erb) Shows listing indivudally
<div class="show_listing">
<div class="col-md-6">
<div class="col-md-6">
<h3><%= @listing.title %></h3>
<h3><%= @listing.location %></h3>
<p><%= @listing.description %></p><br>
<div class="center">
<%= link_to "Apply Now", '#', class: "btn btn-info", data: {no_turbolink: true} %>
</div>
</div>
</div>
</div>
<div class="show_link_position">
<% if current_user == @listing.user %>
<%= link_to 'Edit', edit_listing_path, class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', current_user, class: "btn btn-link" %>
</div>
listing index file (index.html.erb)
<div class="top">
<div class="categories-container">
<div class="boxed grid-3 category-link ">
<li><%= link_to "Find Jobs", findjobs_path, class: "category-link"%></li>
<li><%= link_to "Post Jobs", new_path, class: "category-link" %></li>
<li><%= link_to "Find Jobs", findjobs_path, class: "category-link" %></li>
<li><%= link_to "Post Jobs", new_path, class: "category-link" %></li>
<li><%= link_to "Find Jobs", findjobs_path, class: "category-link" %></li>
<li><%= link_to "Post Jobs", new_path, class: "category-link" %></li>
<li><%= link_to "Find Jobs", findjobs_path, class: "category-link" %></li>
<li><%= link_to "Post Jobs", new_path, class: "category-link" %></li>
</div>
</div>
<div class="table-container">
<div class= "grid-8 grid-moved">
<% @listings.each do |listing| %>
<h4><%= listing.title %></h4>
<h5> <%= listing.user.name %>, Posted <%= time_ago_in_words(listing.created_at) %> ago</h5>
<h5>Job Description:</h5>
<p><%= listing.description %></p>
<p>Location: <%= listing.location %></p>
<li><%= link_to "Show", user_listing_path(name: @user.name, id: listing.id) %></li>
<br><hr><br>
<% end %>
</div>
</div>
<div class="container">
<div class="pagination">
<%= will_paginate @listings %>
</div>
</div>
routes
Rails.application.routes.draw do resources :users resources :sessions, only: [:new, :create, :destroy] resources :listings
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via:'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/new', to: 'listings#new', via: 'get'
match '/users/:name/:id', to: 'listings#show', via: :get, as: :user_listing
match '/findjobs', to: 'listings#index', via: 'get'
Let me know if I forgo any important information.