简体   繁体   中英

Ruby on Rails - One layout page different content

Im pretty new to rails and am looking for guidance.

I am building my portfolio on rails and I know the fundamental rule of rails is "don't repeat yourself."

Can I create a single layout page that displays my work in detail but generates multiple pages, each with its own content. For example: www.mywork.com/nike would have the same layout but different content as www,mywork.com/pepsi. If I do this, how do I pull in certain images/text under their unique name.

Or do I just create static pages and use the same stylesheet?

Thanks for you help!

Take a look at this rails cast - http://railscasts.com/episodes/117-semi-static-pages

I agree that what you are trying to do isn't really 'the rails way', but I use the techniques in the above link whenever I am creating pages such as about us and privacy pages which generally have a block of text and will potentially be updated in the future.

In a nutshell what you have to do is create a model called content_page which has a link (string), and content (text) and heading (string). Then you create a controller with just a show action which will look like so -

   class ContentPagesController < ApplicationController

     def show
       @content_page = ContentPage.find_by_permalink(params[:link])
       raise ActiveRecord::RecordNotFound, "Page not found" if @content_page.nil?
     end
   end

And in your view for your show action you can say things like -

   @content_page.content
   @content_page.heading

to get the content and heading for that record (or whatever other attributes you assign to ContentPage).

Then in your routes you can have something like this at the bottom of your routes.rb (you put it at the bottom because it is a greedy route) -

   match ':link' => 'content_pages#show' 

So now you can say www.mywork.com/nike which will find the content page with nike as the link. I'm being very brief because it is well worth watching that rails cast.

Like I said though, the above technique is only good for basic content pages, you should really have a controller for each page, even if it just has an index action :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM