在Rails上渲染不同类别的显示页面并显示类别 [英] Render different show pages with category in ruby on rails

查看:80
本文介绍了在Rails上渲染不同类别的显示页面并显示类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为博客文章渲染不同的显示页面。

I need render different show pages for my blog posts.

我有3种不同的类别:主题,摘要,项目。

I have 3 different categories : themes, snippets, projects.

每个博客帖子必须与这三个类别相关。

Each blog post must related with those three categories.

如果任何人单击该帖子(假设相关类别为摘要),则其显示方式会不同。 .etc ...其他类别也是如此。

If anyone click post(assume related category is snippets), it display in different show...etc... It is same for other categories.

如何使用条件语句。

推荐答案

you can make routes like:

resources :posts, except:[:show]
get 'posts/:id/cat/:category' , to:'posts#show', as: :show

为类别创建部分内容,如下所示:

you have to create partial for categories as follows:


app / views / posts / _themes.html.erb

app/views/posts/_themes.html.erb

app / views /posts/_snippets.html.erb

app/views/posts/_snippets.html.erb

app / views / posts / _projects.html.erb

app/views/posts/_projects.html.erb

然后在控制器的显示动作中。

then in controller's show action.

controllers/posts_controller.rb



 

 def show
   @post = Post.find(params[:id])
   @category = params[:category]
   ...
 end

然后在显示页面上渲染该类别。

Then render that category in show page.

views / posts / show.html.erb

views/posts/show.html.erb

...
<%= render  '#{@category}'%>
 

这篇关于在Rails上渲染不同类别的显示页面并显示类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆