Reputation: 2793
I don't know an awful lot about Rails and I've inherited this project. For the past few days I've been trying to get my head around, 'link_to', and 'routes.rb'. This stuff is driving me mad - I've spent the whole day looking at it, pasting bits of code into bare projects, where it works..but I just don't understand the error I'm getting here, or how to go about solving it, so if you have any ideas....
In my page _signed_in_header.html.erb I have:
<a href="../staticpages/faq">FAQ</a>
In my routes.rb I have:
get "staticpages/faq"
I know this is set up correct, because when I start a sample project from scratch, it works.
But in this particular project I've inherited I get the error:
NoMethodError in Staticpages#faq
Showing /home/christophecompaq/Populisto/app/views/layouts/_signed_in_header.html.erb where line #48 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #48):
45:
46: <div class='search-box'>
47: <%= simple_form_for @review, :url => search_index_path, :method => :post, :html => { :class => 'form-horizontal'} do |f| %>
48:
49: <%= f.input :search_ids, :collection => @data, :as => :grouped_chosen,
50: :group_method => :last, :prompt => false,
51: :input_html => { :class => 'span5', :multiple => true },
Trace of template inclusion: app/views/layouts/_header.html.erb, app/views/layouts/application.html.erb
Rails.root: /home/christophecompaq/Populisto
Application Trace | Framework Trace | Full Trace
app/views/layouts/_signed_in_header.html.erb:48:in `_app_views_layouts__signed_in_header_html_erb___586079249_69970641688720'
app/views/layouts/_header.html.erb:1:in `_app_views_layouts__header_html_erb__1905506502_69970640142220'
app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___1868096314_69970642536740'
Edit: I was asked to show my review controller code, so here it goes:
class ReviewsController < FrontEndController
respond_to :html, :json
before_filter :with_google_maps_api
def index
@review = Review.new
end
def create
@review = Review.create((params[:review] || {}).merge(:user_id => current_user.id))
if @review.save
redirect_to landing_page, :notice => I18n.t('write_review.review_successfully_created')
else
render :action => :index
end
end
def show
@review = Review.find(params[:id])
end
def edit
@review = Review.find(params[:id])
end
def update
@review = Review.find(params[:id])
if @review.update_attributes(params[:review])
else
render :edit
end
end
def destroy
@review = Review.find(params[:id])
@review.destroy
end
def repost
@review = Review.find(params[:id])
@review.repost(current_user)
end
def reject
@review = Review.find(params[:id])
current_user.reject @review
end
end
Anyway, if you have any ideas what could be wrong, I'd be delighted to know....Thanks.
Christophe.
Upvotes: 1
Views: 5965
Reputation: 13181
in your route file, use this code
get "staticpages/faq", :as => 'faq_page'
The 'as' will generate 2 helper functions: faq_page_url
and faq_page_path
that you can use in your code
Upvotes: 4
Reputation: 898
i hope this will help, i think we have the same issue but i've managed to fix this using this:
in my routes.rb
match 'pages/:action', :controller => "pages"
and in my view:
= link_to "About", {:controller => 'pages', :action => 'about'}
Upvotes: 2
Reputation: 83
The error is happening during rendering of the layout template, not the controller view.
If you're testing the faq page you'll be hitting the StaticpagesController, not the ReviewController you pasted right? And presumably StaticpagesController does not set @review... hence your exception.
So either try wrapping the search box code in a conditional like:
<% if @review %>
... put your review search form here ...
<% end %>
or if the search is supposed to present on all pages, ensure it's populated on all pages. Maybe add a before_filter
on your base controller class with something like
class ApplicationController < ....
before filter :ensure_review_set
private
def ensure_review_set
@review ||= Review.new
end
end
The search form is also referencing @data
if the search_ids
field. That will also need to be initialised by any controllers using this layout.
More generally, if your version of rails supports it, I'd very very highly recommend the better_errors gem for quickly debugging errors such as this.
Upvotes: 1