【问题标题】:using link_to displays path as link使用 link_to 将路径显示为链接
【发布时间】:2015-07-08 18:16:35
【问题描述】:

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

```

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>
-->

```

【问题讨论】:

  • 试试这个&lt;%= link_to article.title, article %&gt; 而不是&lt;%= link_to article.title, [@sub, article] %&gt;

标签: html ruby-on-rails link-to


【解决方案1】:

在你给我更多信息之后,

我认为这是你的问题:

@sub.articles.build

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

【讨论】:

  • 感谢您的帮助。我怎么能解决这个问题?
  • 当你循环浏览你的文章时,你只能得到持久化的文章:@articles.select(&:persisted?).each ...
猜你喜欢
  • 2011-06-01
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多