如何显示个别列表? Ruby on Rails

我目前正在开发一个简单的网站,人们可以在其中列出post。 大多数代码都基于Michael Hartl的教程。 我希望用户能够单击显示列表的链接。 目前,每个用户的列表都在下面找到

http://localhost:3000/users/id 

每个列表都有自己的ID这是我的路线

 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' 

这是我的listing_controller.rb

 class ListingsController < ApplicationController before_action :signed_in_user, only: [:create, :destroy, :edit] before_action :correct_user, only: :destroy 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 show @listing = Listing.find(params[:id]) end def new @listing = Listing.new @listings = Listing.paginate(page: params[:page]) end def destroy @listing.destroy redirect_to current_user 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 def current_listing @listings = listing.find(params[:id]) end end 

我还在列表文件夹列表下为每个列表创建了一个显示页面。 节目页面本身有效。 这是列表的show.html.erb

 


现在,我希望在用户页面(每个列表下方)上有一个链接,可以单独链接到每个post。

我正在寻找这样的东西如何显示个别微博的链接? (铁轨上的ruby3)

谢谢。如果您需要更多信息,请告诉我

要从您将列表集合作为@listings的页面创建链接,您可能会有类似这样的内容……

 
    <% @listings.each do |listing| %>

    <%= listing.title

    <%= listing.location

    <%= listing.description

    <%= link_to "Show", listing, class: "btn btn-link" %>
    <% end %>

以下是我为使其工作而做的事情(借助以下stackoverflowpost如何显示单个微博的链接?(ruby on rails 3))

路线rb。

 match '/users/:id/:id', to 'listings#show', via: :get, as: :user_listing 

用户查看文件

添加链接

 
  • <%= link_to "Show", user_listing_path(id: @user.id, id: listing.id) %>
  • 更改了我的listing_controller文件

     def show @user = User.find_by_id(params[:id]) @listing = Listing.find_by_id(params[:id]) end