route, anchor, function and render
I'm quite a noob in RubyOnRails but I would like to write the description of an object in my homepage if a parameter is written in the url.
For example :
wwW.myWebsite.com -> display the homepage开发者_运维技巧
www.myWebsite.com/demos -> display the homepage with the description field of the object demos
I have done :
-in route.rb
match 'demos/:id' => 'demos#display_description'
-in demo controller
def display_demo
@demo = Demo.find_by_slug(params[:id])
end
but how to load the homepage (which will have to know the demo variable...) ?
thanks a lot
Create a controller for your homepage, ie. :
class HomeController < ApplicationController
def index
@demos = Demo.all # this will load all your demos in an array, or Demo.paginate :page => params[:page] if you want pagination from will_paginate for example.
end
end
in your routes, route the root to your HomeController :
root :to => "home#index"
and voila!
Read the guides from http://guides.rubyonrails.org for more info if you're a rails beginner.
精彩评论