A
A
Alexander2014-05-01 16:33:41
Ruby on Rails
Alexander, 2014-05-01 16:33:41

Dependence of layouts on the rubric, how to implement?

Hello!
I will briefly describe the task, there is a magazine on rails, it has headings, 5 main headings and more than 15 subheadings in which posts are already stored. Question - How to implement a feature so that a journalist, when creating a post, can select a subheading and automatically determine his layout for it (there can be many layouts).
There is also a second question, it is somehow connected with the first one, is it possible to attach your own _partials to this functionality that monitor the type of post in the feed?
Here is an example for the second question:
Image%202014-04-28%20at%208.27.16%20%D0%

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew Emelianenko, 2014-05-06
@Aice

Function to set the layout:

class Post
  layout :set_layout

  private
  def set_layout
    "#{category_name}_layout"
  end
end

To render a post, with a custom view, you can do something like this:
<%= render partial: "posts/#{@post.category_name}", locals: { post: @post } %>

A
Arkady Butermanov, 2014-05-01
@Arkadey

Let's say you have a structure like this:

class Post < ActiveRecord::Base
  belongs_to :category
  belongs_to :sub_category
end

Then, to render an article in a specific layout, depending on the subcategory, you can do something like this:
class PostsController < ApplicationController
  def show
    @post = Post.find(params[:id])
    render layout: @post.sub_category.name
  end
end

And of course, the views/layouts directory should contain the corresponding files for each subcategory.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question