Ruby on rails 显示当前类别-轨道

Ruby on rails 显示当前类别-轨道,ruby-on-rails,categories,Ruby On Rails,Categories,我需要在帖子索引上显示当前类别名称 代替“Jubotron”,它应该显示测试(当选择测试类别时) 我的代码 控制器 def index @categories = Category.all cate = params[:cate] if !cate.nil? @blogs = Category.find(params[:cate]).blogs else @blogs = Blog.all end end def

我需要在帖子索引上显示当前类别名称

代替“Jubotron”,它应该显示测试(当选择测试类别时)

我的代码

控制器

  def index
    @categories = Category.all
    cate = params[:cate]

    if !cate.nil?
      @blogs = Category.find(params[:cate]).blogs
    else
      @blogs = Blog.all
    end

  end

  def show
    @blog = Blog.find(params[:id])
  end

  def new
    @blog = Blog.new
  end

  def create
    @blog = Blog.create(blog_params)

    if @blog.save
      redirect_to @blog
    else
      render :new
    end
  end

  def edit
    #finds the blog id
    @blog = Blog.find(params[:id])
  end

def update
  @blog = Blog.find(params[:id])
  if @blog.update(blog_params)
    redirect_to @blog
  else
    render :edit
  end
end

def destroy
  @blog = Blog.find(params[:id])

  @blog.destroy
  redirect_to :action => :index
end

  private
  def blog_params
    params.require(:blog).permit(:title, :content, category_ids: [])
  end
end
blogs/index.html.erb

  <div class="row">

    <div class="col-md-8">
      <h1 style="text-align:center">Jumbotron</h1>
<%= @categories.each do |category|  %>
<%= category.category %>
<% end %>

      <div class="row">

        <% @blogs.each do |blog| %>
          <div class="col-md-6">
            <div class="card mt-3">
              <div class="card-body">
                <h3><%= link_to blog.title, blog_path(blog.id) %></h3>
                <p><%= blog.content %></p>
                <%= link_to "Pokaż", blog_path(blog.id), class: 'btn btn-primary' %>
                <br></br>
                <div class="">
                  Kategoria:
                  <% blog.categories.each do |category| %>
                  <a><%= link_to category.category,blogs_path(:cate => category.id) %></a>
                  <% end %>
                </div>

              </div>
            </div>

          </div>
            <% end %>

        </div>
      </div>

<div class="col-md-4">
  <div class="card mt-3">
    <div class="card-header">
      Kategorie:

    </div>
    <div class="card-body">

      <ul class="list-group">
        <% @categories.each do |category| %>

  <li class="list-group-item d-flex justify-content-between align-items-center">
    <%= link_to category.category, blogs_path(:cate => category.id) %>

    <span class="badge badge-primary badge-pill"><%= category.blogs.count %></span>
  </li>

  <% end %>

      </ul>
    </div>

  </div>

</div>


  </div>
</div>

巨无霸



卡蒂戈里亚: 类别(id)%> 卡蒂戈里:
  • 类别(id)%>
例如,我可以在categories/show.htm.erb中显示类别名称

但当我把它放到Blogs索引中时,它给了我一个错误,category方法是未定义的

  <strong>Category:</strong>
  <%= @category.category %>
</p> ```


Any ideas?


类别:

``` 有什么想法吗?
在索引页上,您正在循环浏览类别
,因此您应该使用
category.category
,而不是
@category.category
在块内

您的问题是您放置“Jumbotron”代码的地方。目前,您正在硬编码“Jumbotron”一词,如下所示:

<h1 style="text-align:center">Jumbotron</h1>
Jumbotron
您应该将其移动到迭代类别的代码中:

<% @categories.each do |category|  %>
  <h1 style="text-align:center"><%= category.category %></h1>
<% end %>


请记住,如果您有多个类别,这将为您提供多个
标记;每个类别一个。

哦,谢谢,我如何才能只显示一个确切的类别?您必须找到类别并将其存储在控制器的实例变量中,然后在视图中使用该实例变量,或者可以使用
.first
.last
方法来获取第一个或最后一个类别。
<% @categories.each do |category|  %>
  <h1 style="text-align:center"><%= category.category %></h1>
<% end %>