我有个烦人的问题。
我在我的主页上使用了这一行代码,它可以工作,但是在我的帖子/索引页面上,它不起作用。
<%= link_to post.title, user_post_path(post.user.user_name, post), class:"post-listing-home-title" %>最后,我希望在post索引页面上呈现来自用户的所有帖子,链接到他们的帖子,如下所示
本地主机:3000/users/user_name/post/“post名称”
这是我的错误
显示第10行所在的/Users/****/rorapps/*****/app/views/posts/index.html.erb:
没有路由匹配{:action=>"show",:controller=>“post”,:id=>"eli-the-great",:user_id=>nil}缺少必需的键::user_id
这是我的密码
Posts控制器
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /posts
# GET /posts.json
def index
@posts = Post.all.order("created_at desc")
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = current_user.posts.build
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = current_user.posts.build(post_params)
respond_to do |format|
if @post.save
flash[:notice] = "Post successfully created"
format.html { redirect_to @post }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
flash[:notice] = "Post successfully created"
format.html { redirect_to @post }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :content,:slug, :metadescription, :focuskeyword)
end
endposts/index.html.erb
<p id="notice"><%= notice %></p>
<h1 class="post-listing">Listing Posts</h1>
<% @posts.each do |post| %>
<p class="date"><%= post.created_at.strftime('%A, %B %d') %></p>
<%= link_to post.title, user_post_path(post.user.user_name, post), class:"post-listing-title" %>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>welcome.html.erb --这是代码行工作的地方
<div class="container">
<% @posts.each do |post| %>
<div class="featured-post fadeInBlock animated">
<p class="date"><%= post.created_at.strftime('%A, %B %d') %></p>
<span class="show-right"><%= link_to '', user_post_path(:user_id, post), class:"myright fa fa-chevron-right" %></span>
<%= link_to post.title, user_post_path(post.user.user_name, post), class:"post-listing-home-title" %>
<% if user_signed_in? %>
<td><%= link_to 'Edit', edit_user_post_path(:user_id,post) %></td>
<td><%= link_to 'Destroy', user_post_path(:user_id, post), method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</div>
<% end %>
<div id="about-home">
<div id="about-box">
<h1 id="abouttext">Try Harder !</h1>
</div>
</div>谢谢大家的帮助
发布于 2016-05-28 00:19:34
问题是这条线
<%= link_to post.title, user_post_path(post.user.user_name, post), class:"post-listing-title" %>您将在循环中呈现这些链接。
<% @posts.each do |post| %>所以你的一个帖子作者似乎没有user_name。
https://stackoverflow.com/questions/37493907
复制相似问题