【问题标题】:how to display individual listings? Ruby on Rails如何显示单个列表? Ruby on Rails
【发布时间】:2014-08-05 23:53:17
【问题描述】:

我目前正在开发一个简单的网站,人们可以在其中列出帖子。大部分代码基于 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

<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', '#', class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', current_user, class: "btn btn-link" %>
</div>

现在我想在用户页面(在每个列表下)有一个链接,可以单独链接到每个帖子。

我正在寻找这样的东西 how to display a link to individual microposts? (ruby on rails 3)

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

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

    <ol class="listings">
      <% @listings.each do |listing| %>
        <div class="listing">
          <h2><%= listing.title</h2>
          <h3><%= listing.location</h3>
          <p><%= listing.description</p>
          <%= link_to "Show", listing, class: "btn btn-link" %>
        </div>
      <% end %>
    </ol>
    

    【讨论】:

    • 这是个好主意,但我想链接到每个单独的列表。列表由标题位置和描述组成
    • 我建议的代码会发出一个“列表”,其中包含指向@listing 集合中所有列表的链接。你试过我的建议了吗?
    • 我想创建一个链接(位于显示显示的列表集合中的每个列表下方),单击该链接将打开一个仅显示该特定列表的新显示页面。我尝试了您的建议,它创建了一个带有列表标题的链接列表,但这并不是我想要的。
    • 好的...我只是尽量保持答案简短,并专注于获取链接的基本问题。我想你可以自己添加列表的其余部分。
    • 很抱歉,我没有那么快理解它,但我对 Rails 很陌生。当我使用链接时,我收到以下错误 undefined local variable or method `listing'
    【解决方案2】:

    这是我为使其工作所做的工作(借助以下 stackoverflow 帖子如何显示指向单个微帖子的链接?(ruby on rails 3))

    路线 rb.

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

    用户查看文件

    添加链接

    <li><%= link_to "Show", user_listing_path(id: @user.id, id: listing.id) %></li>
    

    更改了我的 listing_controller 文件

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

    【讨论】:

    • 我觉得你有问题。这表明用户的ID和listing ID是一样的???你通常会有类似'/users/:user_id/:id' 这样你的控制器可以在params 中分别找到它们。
    • 嗯...我不确定。它虽然有效。用户 ID 和列表 ID 也不应该相同。有一个用于列表的表和一个用于用户的表。列表和用户有自己的 ID。
    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2012-11-24
    相关资源
    最近更新 更多