iFurniture iFurniture - 3 months ago 11
Ruby Question

NoMethodError in Refinery::Blog::Posts#index

When I want to go to "localhost:3000/blog" the web page gives this error...

Showing C:/Sites/ifurniture/app/views/refinery/blog/posts/index.html.erb where line #3 raised:

undefined method `to_sym' for {:title=>"Body", :slug=>"body"}:Hash

Rails.root: C:/Sites/ifurniture


this is the blog controller..

module Refinery
module Blog
class PostsController < BlogController

before_filter :find_all_blog_posts, :except => [:archive]
before_filter :find_blog_post, :only => [:show, :comment, :update_nav]
before_filter :find_tags

respond_to :html, :js, :rss

def index
if request.format.rss?
@posts = if params["max_results"].present?
# limit rss feed for services (like feedburner) who have max size
Post.recent(params["max_results"])
else
Post.newest_first.live.includes(:comments, :categories)
end
end
respond_with (@posts) do |format|
format.html
format.rss { render :layout => false }
end
end

def show
@comment = Comment.new

@canonical = refinery.url_for(:locale => Refinery::I18n.current_frontend_locale) if canonical?

@post.increment!(:access_count, 1)

respond_with (@post) do |format|
format.html { present(@post) }
format.js { render :partial => 'post', :layout => false }
end
end

def comment
@comment = @post.comments.create(comment_params)
if @comment.valid?
if Comment::Moderation.enabled? or @comment.ham?
begin
CommentMailer.notification(@comment, request).deliver_now
rescue
logger.warn "There was an error delivering a blog comment notification.\n#{$!}\n"
end
end

if Comment::Moderation.enabled?
flash[:notice] = t('thank_you_moderated', :scope => 'refinery.blog.posts.comments')
redirect_to refinery.blog_post_url(params[:id])
else
flash[:notice] = t('thank_you', :scope => 'refinery.blog.posts.comments')
redirect_to refinery.blog_post_url(params[:id],
:anchor => "comment-#{@comment.to_param}")
end
else
render :show
end
end

def archive
if params[:month].present?
date = "#{params[:month]}/#{params[:year]}"
archive_date = Time.parse(date)
@date_title = ::I18n.l(archive_date, :format => '%B %Y')
@posts = Post.live.by_month(archive_date).page(params[:page])
else
date = "01/#{params[:year]}"
archive_date = Time.parse(date)
@date_title = ::I18n.l(archive_date, :format => '%Y')
@posts = Post.live.by_year(archive_date).page(params[:page])
end
respond_with (@posts)
end

def tagged
@tag = ActsAsTaggableOn::Tag.find(params[:tag_id])
@tag_name = @tag.name
@posts = Post.live.tagged_with(@tag_name).page(params[:page])
end

private

def comment_params
params.require(:comment).permit(:name, :email, :message)
end

protected
def canonical?
Refinery::I18n.default_frontend_locale != Refinery::I18n.current_frontend_locale
end
end
end
end


the post controller...

module Refinery
module Blog
class PostsController < BlogController

before_filter :find_all_blog_posts, :except => [:archive]
before_filter :find_blog_post, :only => [:show, :comment, :update_nav]
before_filter :find_tags

respond_to :html, :js, :rss

def index
if request.format.rss?
@posts = if params["max_results"].present?
# limit rss feed for services (like feedburner) who have max size
Post.recent(params["max_results"])
else
Post.newest_first.live.includes(:comments, :categories)
end
end
respond_with (@posts) do |format|
format.html
format.rss { render :layout => false }
end
end

def show
@comment = Comment.new

@canonical = refinery.url_for(:locale => Refinery::I18n.current_frontend_locale) if canonical?

@post.increment!(:access_count, 1)

respond_with (@post) do |format|
format.html { present(@post) }
format.js { render :partial => 'post', :layout => false }
end
end

def comment
@comment = @post.comments.create(comment_params)
if @comment.valid?
if Comment::Moderation.enabled? or @comment.ham?
begin
CommentMailer.notification(@comment, request).deliver_now
rescue
logger.warn "There was an error delivering a blog comment notification.\n#{$!}\n"
end
end

if Comment::Moderation.enabled?
flash[:notice] = t('thank_you_moderated', :scope => 'refinery.blog.posts.comments')
redirect_to refinery.blog_post_url(params[:id])
else
flash[:notice] = t('thank_you', :scope => 'refinery.blog.posts.comments')
redirect_to refinery.blog_post_url(params[:id],
:anchor => "comment-#{@comment.to_param}")
end
else
render :show
end
end

def archive
if params[:month].present?
date = "#{params[:month]}/#{params[:year]}"
archive_date = Time.parse(date)
@date_title = ::I18n.l(archive_date, :format => '%B %Y')
@posts = Post.live.by_month(archive_date).page(params[:page])
else
date = "01/#{params[:year]}"
archive_date = Time.parse(date)
@date_title = ::I18n.l(archive_date, :format => '%Y')
@posts = Post.live.by_year(archive_date).page(params[:page])
end
respond_with (@posts)
end

def tagged
@tag = ActsAsTaggableOn::Tag.find(params[:tag_id])
@tag_name = @tag.name
@posts = Post.live.tagged_with(@tag_name).page(params[:page])
end

private

def comment_params
params.require(:comment).permit(:name, :email, :message)
end

protected
def canonical?
Refinery::I18n.default_frontend_locale != Refinery::I18n.current_frontend_locale
end
end
end
end


and this is the index.html.erb of Blog.

<section class="container">
<% content_for :body do %>
<%= raw @page.content_for(Refinery::Pages.default_parts.first.to_sym) if Refinery::Pages.default_parts.any? %>

<% if @posts.any? %>
<section id="blog_posts" class="news">
<%= render :partial => "/refinery/blog/shared/post", :collection => @posts %>
<%= will_paginate @posts %>
</section>
<% else %>
<p><%= t('.no_blog_articles_yet') %></p>
<% end %>
<% end %>

<% content_for :side_body_prepend do -%>
<%= raw @page.content_for(Refinery::Pages.default_parts.second.to_sym) %>
<% end if Refinery::Pages.default_parts.many? -%>

<%= render "/refinery/content_page" %>
<% content_for :stylesheets, stylesheet_link_tag('refinery/blog/frontend') %>
</section>


I'll be watching for your help, thanks.

Answer

You can use to_sym method on hash or string so in your code you can do something like this:

index.html

instead this:

<%= raw @page.content_for(Refinery::Pages.default_parts.first.to_sym) if Refinery::Pages.default_parts.any? %>

put this:

<%= raw @page.content_for(Refinery::Pages.default_parts.first[:title].to_sym) if Refinery::Pages.default_parts.any? %>

instead [:title]you can also use [:slug]