Josh Hadik
Josh Hadik

Reputation: 433

Is it wrong to use partials in this way?

Ok so I am developing a website in rails right now. Basically it is a static site (but I am using rails because eventually my client wants blog and post functionality added in), which has a bunch of different sections with different articles in each section. Each article is designed with it's own html structure, some have tables, divs, font coloring, etc. so storing these articles as text in the database doesn't make sense. However, it also didn't seem to make sense to have an entirely separate html file for each article in each section when they'r parent element is going to have the same common structure (what I mean by this, is each section on the site, like about, or publications, are going to have they're own unique layout, however, every article belonging to a given section will be placed in the same div as their sister articles.

So I had an idea. I thought I would use partials for rendering unique content in the same layout for each section. (keep in mind that nothing is currently stored in a database.) So I am thinking of setting the site up to have different view folders for the sections, and, inside each of those view folders I will have another folder titled "content," in which I will put the specific html for a given route.

For example, one of my 'about' routes looks like this

    get 'about/:pagename', to: 'about#show'

Which sends the parameter :pagename over to the controller for the show method, at which point I capture that parameter into a variable (in the about_controller.rb file)

    def show
      @content = params[:pagename].downcase
    end

And then the show.html.erb view renders a partial with the same name as the @content variable.

    <div><%= render partial: "about/content/#{@content}" %></div>

At this point, the view looks inside the content folder for the specific partial it is called with, and renders that html.

My question is.... Is this unconventional? I am pretty sure I know the answer to that question... which is yes. So my follow up question is how bad is it? Are there any huge website-shattering problems this will cause that I can't forsee with my limited rails experience? Is there a much simpler, much more conventional method to displaying the content that I have overlooked?

Upvotes: 1

Views: 73

Answers (1)

Mario
Mario

Reputation: 1359

That's a pretty interesting way to do it, but what you're describing is pretty much a CMS, and there are already quite a few out there that would be a good fit for you.

Off the top of my head two popular ones are Locomotive CMS and Radiant CMS

No need to reinvent the wheel! They support static pages, too.

Upvotes: 1

Related Questions