如何显示单个列表?Ruby on Rails
[英] how to display individual listings? Ruby on Rails
本文介绍了如何显示单个列表?Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在一个简单的网站上工作,人们可以在其中列出帖子.大部分代码基于 Michael Hartl 的教程.我希望用户能够单击单独显示列表的链接.目前,每个用户的列表都可以在
下找到
http://localhost:3000/users/id
每个列表都有自己的 ID这是我的路线
Rails.application.routes.draw 做资源:用户资源:会话,仅:[:new, :create, :destroy]资源:列表根'static_pages#home'匹配'/signup',到:'users#new',通过:'get'匹配'/signin',到:'sessions#new',通过:'get'匹配 '/signout',到:'sessions#destroy',通过:'delete'匹配'/help',到:'static_pages#help',通过:'get'匹配'/contact',到:'static_pages#contact',通过:'get'匹配'/about',到:'static_pages#about',通过:'get'匹配'/new',到:'listings#new',通过:'get'
这是我的listing_controller.rb
class ListingsController <应用控制器before_action :signed_in_user, only: [:create, :destroy, :edit]before_action :correct_user, only: :destroy定义创建@listing = current_user.listings.build(listing_params)如果@listing.saveflash[:success] = "职位发布已创建"重定向到当前用户别的呈现列表/新"结尾结尾定义编辑结尾高清秀@listing = Listing.find(params[:id])结尾定义新@listing = Listing.new@listings = Listing.paginate(page: params[:page])结尾销毁@listing.destroy重定向到当前用户结尾私人的定义列表参数params.require(:listing).permit(:description, :location, :title)结尾定义正确_用户@listing = current_user.listings.find_by(id: params[:id])如果@listing.nil,则重定向到current_user?结尾def current_listing@listings = listing.find(params[:id])结尾结尾
我还在列表列表文件夹下为每个列表创建了一个显示页面.显示页面本身有效.这是清单的 show.html.erb
<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 class="show_link_position"><% if current_user == @listing.user %><%= link_to 'Edit', '#', class: "btn btn-link" %>|<%结束%><%= link_to 'Back', current_user, class: "btn btn-link" %>
现在我想在用户页面(在每个列表下)有一个链接,可以单独链接到每个帖子.
我正在寻找这样的东西如何显示单个微博的链接?(ruby on rails 3)
谢谢如果您需要更多信息,请告诉我
解决方案
这是我为使其工作所做的工作(借助以下 stackoverflow 帖子如何显示指向单个微博的链接?(ruby on rails 3))
路线 rb.
匹配 '/users/:id/:id', 到 'listings#show', via: :get, as: :user_listing
用户查看文件
添加链接
<%= link_to "Show", user_listing_path(id: @user.id, id: listing.id) %></li>
更改了我的列表控制器文件
def 显示@user = User.find_by_id(params[:id])@listing = Listing.find_by_id(params[:id])结尾
I am currently working on a simple website where people can list postings. Most of the code is based on Michael Hartl's tutorial.
I want users to have the ability to click on a link that displays the listing individually.
currently, the listings' of each user are found under
http://localhost:3000/users/id
Every listing has it's own id
Here are my 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'
Here is my 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
I also created a show page for each listing under listings the listings folder.
The show page itself works.
Here is the show.html.erb for the listing
<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>
Now I would like to have a link on the user page (under each listing) that would link to each post individually.
I'm looking for something like this
how to display a link to individual microposts? (ruby on rails 3)
Thank you
Let me know if you need more information
解决方案
Here is what I did to get it to work (with the help of the following stackoverflow post how to display a link to individual microposts? (ruby on rails 3))
Routes rb.
match '/users/:id/:id', to 'listings#show', via: :get, as: :user_listing
users view file
add the link
<li><%= link_to "Show", user_listing_path(id: @user.id, id: listing.id) %></li>
Changed my listing_controller file
def show
@user = User.find_by_id(params[:id])
@listing = Listing.find_by_id(params[:id])
end
这篇关于如何显示单个列表?Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!