使用 link_to 将路径显示为 link

using link_to displays path as link

我正在使用 <% @articles.each do |article| %> do 显示所有有效的文章,但它也将“/subs/27/articles”显示为其中一篇文章。当我单击它时,它会转到我的索引视图。为什么这篇 link 与所有文章

一起显示在这里

```

class SubsController < ApplicationController


  def show
    @sub = Sub.find(params[:id])
    @category = Category.find(params[:category_id])
    @articles = @sub.articles
    @article = @sub.articles.build
  end

  def new
    @category = Category.find(params[:category_id])
    @sub = Sub.new
  end


  def create
    @category = Category.find(params[:category_id])
    @sub = Sub.new(params.require(:sub).permit(:title))
    @sub.category = @category
    if @sub.save
        flash[:notice] = "Subcategory was saved"
        redirect_to [@category, @sub]
    else
        flash[:error] = "The Subcategory could not be saved"
        render :new
    end
  end
  def edit
    @category = Category.find(params[:category_id])
    @sub = Sub.find(params[:id])  
  end

  def update
    @category = Category.find(params[:category_id])
    @sub = Sub.find(params[:id])
    @sub.category = @category
    if @sub.update_attributes(params.require(:sub).permit(:title))
        flash[:notice] = "Subcategory was saved"
        redirect_to [@category, @sub]
    else
        flash[:error]= "The subcategory could not be saved"
        render :edit
    end
  end
  def sub_params
    params.require(:sub).permit(:title)
  end

views/subs/show.html.erb

<h1 align="center"><%= @sub.title %></h1>

<div class="row">
    <div class="col-md-8">
        <%=  link_to "Back", :back, class: "btn btn-default" %>
        <% if user_signed_in? %>
        <%= link_to "Edit", edit_category_sub_path(@category, @sub), class: "btn btn-success" %>
        <% else %>
        <% end %>

    </div>
    <!-- 
    <div class="col-md-4">
        <%= link_to "New Article", new_category_sub_path(@category), class: 'btn btn-success' %>
    </div>
-->
</div>
<hr>
<br>
<div class="row">
    <% @articles.each do |article| %>
    <div class="col-xs-3">
        <div class="tile" >
            <h5 align="center">
                <%= link_to article.title, [@sub, article] %>
            </h5>
        </div>
    </div>
    <% end %>
</div>

<% if user_signed_in? %>
<div class="col-md-12">
    <%= render 'articles/form', category: @category, article: @article, sub: @sub %>
</div>
<% else %>
<% end %>
    <!-- 
    <div class="col-md-12">
        <%= render 'articles/form', category: @category, article: @article, sub: @sub %>
    </div>
-->

```

你给了我更多信息后,

我认为这是你的问题:

@sub.articles.build

它正在建立一个新的未保存文章关联,然后它会遍历文章,包括那条未保存的记录,因此标题为空。