【发布时间】: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