在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}'%>