【发布时间】:2016-11-12 03:57:08
【问题描述】:
我正在构建一个作为播客目录的 Rails 应用程序。我有播客和剧集。 Episode 属于 Podcast,Podcast 有很多 Episode。在主页上,我想显示最近创建的 5 集并链接到它们。
我可以使用它,尽管这显然不是这样做的方法:
<% @episodes.each do |episode| %>
<%# link_to episode do %>
<a href="http://example.com/podcasts/<%= episode.podcast_id %>/episodes/<%= episode.id %>" class="tt-case-title c-h5"><%= episode.title %></a>
<%# end %>
<% end %>
link_to 已被注释掉,因为这是我的问题的一部分。
这里是索引控制器:
def index
@podcasts = Podcast.where.not(thumbnail_file_name: nil).reverse.last(5)
@episodes = Episode.where.not(episode_thumbnail_file_name: nil).reverse.last(5)
end
这里是路由文件:
Rails.application.routes.draw do
devise_for :podcasts
resources :podcasts, only: [:index, :show] do
resources :episodes
end
authenticated :podcast do
root 'podcasts#dashboard', as: "authenticated_root"
end
root 'welcome#index'
end
rake routes | grep episode的结果:
podcast_episodes GET /podcasts/:podcast_id/episodes(.:format) episodes#index
POST /podcasts/:podcast_id/episodes(.:format) episodes#create
new_podcast_episode GET /podcasts/:podcast_id/episodes/new(.:format) episodes#new
edit_podcast_episode GET /podcasts/:podcast_id/episodes/:id/edit(.:format) episodes#edit
podcast_episode GET /podcasts/:podcast_id/episodes/:id(.:format) episodes#show
PATCH /podcasts/:podcast_id/episodes/:id(.:format) episodes#update
PUT /podcasts/:podcast_id/episodes/:id(.:format) episodes#update
DELETE /podcasts/:podcast_id/episodes/:id(.:format) episodes#destroy
如何使用直接链接到剧集的 link_to 正确创建标题的文本链接?谢谢!
【问题讨论】:
标签: ruby-on-rails routes link-to