首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails user_post_path(post.user.user_name,post)缺少必需的键:[:user_id]不工作在post索引页面上,而是在欢迎页面上工作

rails user_post_path(post.user.user_name,post)缺少必需的键:[:user_id]不工作在post索引页面上,而是在欢迎页面上工作
EN

Stack Overflow用户
提问于 2016-05-28 00:04:49
回答 1查看 79关注 0票数 0

我有个烦人的问题。

我在我的主页上使用了这一行代码,它可以工作,但是在我的帖子/索引页面上,它不起作用。

代码语言:javascript
复制
<%= 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控制器

代码语言:javascript
复制
    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
end

posts/index.html.erb

代码语言:javascript
复制
    <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 --这是代码行工作的地方

代码语言:javascript
复制
    <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>

谢谢大家的帮助

EN

回答 1

Stack Overflow用户

发布于 2016-05-28 00:19:34

问题是这条线

代码语言:javascript
复制
<%= link_to post.title, user_post_path(post.user.user_name, post), class:"post-listing-title" %>

您将在循环中呈现这些链接。

代码语言:javascript
复制
<% @posts.each do |post| %>

所以你的一个帖子作者似乎没有user_name。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37493907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档