【发布时间】:2016-07-05 06:08:07
【问题描述】:
我在我的应用程序的根目录中创建了一个最新的帖子框。在这个框中,我有属于主题的帖子。也许这是一个愚蠢的问题,但我怎样才能将点击帖子重定向到它所属的主题?
这会将我重定向到 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
【问题讨论】:
-
topic_path(post.topic)?您能否发布您的路线以使我们更加清楚。 -
我已经尝试过这种方式@Justin,但出现错误:
undefined local variable or methodpost'` 我更新了路线。 -
您不能遍历
posts。请参考this Rails Guides post。 -
我添加了一个答案以反映上面评论中提到的更改。它应该让你找到你的解决方案。干杯。
标签: ruby-on-rails ruby-on-rails-4