【问题标题】:Model Association模型协会
【发布时间】:2012-05-09 09:01:36
【问题描述】:

嗨,我在关联方面遇到了一点问题,我正在使用 Rails 3.2,我想创建一个特殊的博客,这个博客有很多部分,这个部分有很多文章,一篇文章属于一个类别。所以我的模型是:

class Article < ActiveRecord::Base
  belongs_to :section
  belongs_to :category

class Category < ActiveRecord::Base
  has_many :articles

class Section < ActiveRecord::Base
  has_many :article

所以文章 belongs_to 部分,在 routes.rb 中:

 resources :sections do
   resources :articles
 end

耙子路线:

                     POST   /sections/:section_id/articles(.:format)          articles#create
 new_section_article GET    /sections/:section_id/articles/new(.:format)      articles#new
edit_section_article GET    /sections/:section_id/articles/:id/edit(.:format) articles#edit
     section_article GET    /sections/:section_id/articles/:id(.:format)      articles#show
                     PUT    /sections/:section_id/articles/:id(.:format)      articles#update
                     DELETE /sections/:section_id/articles/:id(.:format)      articles#destroy
            sections GET    /sections(.:format)                               sections#index
                     POST   /sections(.:format)                               sections#create
         new_section GET    /sections/new(.:format)                           sections#new
        edit_section GET    /sections/:id/edit(.:format)                      sections#edit
             section GET    /sections/:id(.:format)                           sections#show
                     PUT    /sections/:id(.:format)                           sections#update
                     DELETE /sections/:id(.:format)                           sections#destroy

所以我的问题是如何创建具有索引和显示操作的 Categories_controller。 显示属于该类别的文章,并在文章路径的视图(Category#show)中有一个 link_to。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    假设您确实需要嵌套路由来适应您的域模型(例如,文章需要根据它们是在类别或部分的上下文中查看而有所不同),那么您可以创建另一组嵌套路由像这样的路线:

    routes.rb

    resources :categories, only: [:index, :show] do
      resources :articles
    end
    

    这将设置路线以按类别查找您的类别和文章,但您必须在 ArticlesController 上的 params[:category_id]params[:section_id] 中分叉您的逻辑,如下所示:

    class ArticlesController < ApplicationController
      def index
        if params[:section_id]
          # handle section_articles_path to display articles by section
        elsif params[:category_id]
          # handle category_articles_path to display articles by category
        else
          # handle articles_path to display all articles (assuming you have resources :articles in routes)
        end
      end
      # ...
    end
    

    然后,基于类别显示文章的链接将使用为您创建的生成的路线帮助方法。

    categories/show.html.erb

    <ul>
      <% @category.articles.each do |article| %>
        <li><%= link_to article.title, category_article_path(@category, article) %></li>
      <% end %>
    </ul>
    

    然后,当然,您可以重构视图代码以将集合呈现为它自己的部分而不是手动迭代,但我暂时将其保留...

    如果您不需要以不同的方式处理上下文(通过部分与类别访问文章),我建议只设置三个基本路线(:articles:sections:categories)和从那里出发。

    【讨论】:

    • 谢谢,我认为我的逻辑昨天失败了,我想访问没有类别路径的文章。谢谢你的回答!
    【解决方案2】:

    非常简单 match 'catagories' =&gt; "catagories#index"match 'catagories/show/:id' =&gt; "catagories#show" 在节目中@articles = Article.where("category_id",params[:id])

    @articles = Article.where("category_id",params[:id])这将解决你的目的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多