我创建了一个用户个人资料页面,其中显示了用户创建的所有列表。每个商家信息都有一个' show'链接到新页面的链接,该页面在不同页面上单独显示列表。我让这个用于用户个人资料页面。 我正在使用此链接
<li><%= link_to "Show", user_listing_path(name: @user.name, id: listing.id) %></li>
但是,现在我想创建一个列表索引页面,显示每个用户的所有列表。每个列表都应该有一个&#39; show&#39;链接到单独显示列表的页面的链接。在用户个人资料页面上工作的相同链接在列表索引页面上不起作用。
我得到以下错误未定义方法`name&#39;为零:NilClass 它指向
有谁知道为什么?
用户展示文件(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.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>
列出控制器
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
列出展示文件(show.html.erb)显示单独列出
<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>
列出索引文件(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>
路由
Rails.application.routes.draw做 资源:用户 资源:会话,仅限:[:new,:create,:destroy] 资源:列表
root&#39; static_pages #home&#39;
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'
如果我放弃任何重要信息,请告诉我。
答案 0 :(得分:1)
您尚未在控制器中定义@user
。
在下一行中,您可以正确访问列表用户,如下所示:
<% if current_user?(listing.user) %>
您只需将代码调整为:
<li><%= link_to "Show", user_listing_path(name: listing.user.name, id: listing.id) %></li>
有意义吗?
答案 1 :(得分:0)
尝试:
<li><%= link_to "Show", user_listing_path(@user, listing) %></li>
在你的控制器上:
def show
@user = User.find(params[:id)
@listing = Listing.find(params[:id])
end