Answer the question
In order to leave comments, you need to log in
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:
Answer the question
In order to leave comments, you need to log in
Function to set the layout:
class Post
layout :set_layout
private
def set_layout
"#{category_name}_layout"
end
end
<%= render partial: "posts/#{@post.category_name}", locals: { post: @post } %>
Let's say you have a structure like this:
class Post < ActiveRecord::Base
belongs_to :category
belongs_to :sub_category
end
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
render layout: @post.sub_category.name
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question