Show blog posts in sidebar (rails 3)
I want create a blog posts list in the sidebar.
My BlogsControllerdef bloglist
@blog = Blog.all
render 'bloglist'
end
And I call bloglist.html.erb in layout/application.html.erb:
<%= render "blogs/bloglist" %>
After, I got missing template error:开发者_StackOverflow
Missing partial blogs/bloglist with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths...
Whats wrong?
It seems you have an error on your file naming.
A partial view must always begin with an underscore. In this case, your partial view must be app/views/blogs/_bloglist.html.erb
.
When you call render on a view and pass 'blogs/bloglist', that's the file it's going to look for.
You should also be aware that by calling that partial it will not call the controller action by default. If you want to get the blog list on every action rendering, you should use a before_filter on your ApplicationController.
Something like this:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :get_blog_list
protected
def get_blog_list
@blog = Blog.all
end
end
精彩评论