Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails HABM文章和类别,can';获取类别链接以查看相关文章_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails HABM文章和类别,can';获取类别链接以查看相关文章

Ruby on rails Rails HABM文章和类别,can';获取类别链接以查看相关文章,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我正在制作一个新闻网站,并创建了一些文章和分类模型,这些文章和分类模型已经属于了很多协会 class Category < ActiveRecord::Base has_and_belongs_to_many :articles validates :name, presence: true, length: { in: 2..20 }, uniqueness: true def to_s "#{name}" end end class Article

我正在制作一个新闻网站,并创建了一些文章和分类模型,这些文章和分类模型已经属于了很多协会

class Category < ActiveRecord::Base
  has_and_belongs_to_many :articles

    validates :name, presence: true, length: { in: 2..20 }, uniqueness: true

  def to_s
    "#{name}"
  end
end


class Article < ActiveRecord::Base
  has_many :comments
  has_and_belongs_to_many :categories
end
现在,我设法在索引和显示站点上显示文章和所属类别。我想建立一个类别链接,指向与单个类别相关的文章的站点(e.x.sport=>所有与该类别相关的文章)。在categories-index.html.erb中:

<h1>Categories</h1>

<div class="row">
  <% @categories.each do |category| %>

    <div class='col-sm-3'>
      <h2><%= link_to category %></h2>

      <h3>Articles</h3>
        <% category.articles.each do |article| %>
          <%= link_to article.title, article %>
        <% end %>
    </div>
  <% end %>
</div>
类别
文章
这些链接出现在网站上,但它们不会指向任何东西。如何将这些链接路由到适当的站点

class CategoriesController < ApplicationController
  def index
    @categories = Category.all
  end


  def show
  end

  def new
    @category = Category.new
    @articles = Article.all
  end

  def edit
    @articles = Article.all
  end

  def create
    @category = Category.new(category_params)

    respond_to do |format|
      if @category.save
        format.html { redirect_to @article, notice: 'Category added' }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { redirect_to @article, notice: 'Category not added'}
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { render :show, status: :ok, location: @category }
      else
        format.html { render :edit }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

   def destroy

    @category.destroy
    redirect_to article_path(@article)
  end
  private
    def category_params
      params.require(:category).permit(:name, :article_ids => [])
    end
end
class CategoriesController[])
终止
终止

指向帮助程序的
链接至少需要两个参数,即要显示的内容和要路由到的位置

您的类别链接不包括后者

<%= link_to category %>

首先要突出一英里的是你的
to_s
方法。重新定义类方法是没有好处的。此外,在您的情况下,这是绝对不必要的,因为:a)ruby的语法
“#{variable}”
已经作为String类的实例返回了一个值;b) 我怀疑您的
:name
属性从一开始就是一个字符串。你能更清楚地解释一下你的应用程序和类别/文章交互的逻辑吗?
<%= link_to category %>
<%= link_to category.name, category %>
# CategoriesController
def show
  @category = Category.find(params[:id])
  @articles = @category.articles
end