如何将帖子链接到主题?

时间:2016-03-18 13:06:23

标签: ruby-on-rails ruby-on-rails-4

我在根目录中的应用中创建了最新的帖子框。在这个框中,我有一个属于主题的帖子。也许这是一个愚蠢的问题,但我如何重定向点击帖子到它所属的主题?

这会将我重定向到localhost:3000 / topics,如何将topic_id添加到此路径?

这就是我收到最新帖子的方式:

控制器:

class ApplicationController < ActionController::Base
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception
    before_filter :configure_permitted_parameters, if: :devise_controller?
    helper_method :latest_posts
def latest_posts
      @posts ||= Post.all.order("created_at desc").limit(3)
end
end

指数:

<% latest_posts.each do |posts| %>
  <div class="bs-callout bs-callout-warning">
    <p><%= link_to post.content.html_safe, topic_path(post.topic) %></p>
  </div>
<% end %>

routes.rb中:

  devise_for :users
  get 'categories' => 'categories#index'
  resources :topics
  resources :posts
  resources :users

路线:

new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
              categories GET    /categories(.:format)          categories#index
                  topics GET    /topics(.:format)              topics#index
                         POST   /topics(.:format)              topics#create
               new_topic GET    /topics/new(.:format)          topics#new
              edit_topic GET    /topics/:id/edit(.:format)     topics#edit
                   topic GET    /topics/:id(.:format)          topics#show
                         PATCH  /topics/:id(.:format)          topics#update
                         PUT    /topics/:id(.:format)          topics#update
                         DELETE /topics/:id(.:format)          topics#destroy
                   posts GET    /posts(.:format)               posts#index
                         POST   /posts(.:format)               posts#create
                new_post GET    /posts/new(.:format)           posts#new
               edit_post GET    /posts/:id/edit(.:format)      posts#edit
                    post GET    /posts/:id(.:format)           posts#show
                         PATCH  /posts/:id(.:format)           posts#update
                         PUT    /posts/:id(.:format)           posts#update
                         DELETE /posts/:id(.:format)           posts#destroy
                   users GET    /users(.:format)               users#index
                         POST   /users(.:format)               users#create
                new_user GET    /users/new(.:format)           users#new
               edit_user GET    /users/:id/edit(.:format)      users#edit
                    user GET    /users/:id(.:format)           users#show
                         PATCH  /users/:id(.:format)           users#update
                         PUT    /users/:id(.:format)           users#update
                         DELETE /users/:id(.:format)           users#destroy
                    root GET    /                              categories#index

2 个答案:

答案 0 :(得分:2)

您希望首先遍历posts,然后在topic的单个实例上链接到post

<h5>Here are all of my posts</h5>
<% @posts.each do |post| %>
  <%= link_to post.content.html_safe, topic_path(post.topic) %>
<% end %>

这是一篇关于ActionView和渲染的实体Rails guides帖子。这绝对值得一读。

答案 1 :(得分:0)

假设您已在show中定义了config/routes.rb路线,则可以使用topic_path帮助。

<%= link_to posts.content.html_safe, topic_path(topic) %>

topic是您要重定向到的实例。