Created
July 19, 2011 23:49
-
-
Save richardsondx/1094033 to your computer and use it in GitHub Desktop.
Wasn't able to fix a NoMethodError in Answers#index issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I look for solutions for NoMethodError and tried to switch the @answers by @answers | |
# I tried to redirect the route of answers#index | |
# The issue happen when I click on "Back" from any page of the answer view, after editing # an "Answer" or when i land on "http://localhost:3000/answers" since the back button | |
# redirect me toward this page. I thought the issue could be the name of my path but I | |
# wasn't able to fix it. I am quite confused that @answer is returned as nil when i click # on back | |
#======================================== | |
# Error message I get from : http://localhost:3000/answers | |
#======================================== | |
NoMethodError in Answers#index | |
Showing /Users/Richardson/rails_projects/WRLY/app/views/answers/index.html.erb where line #11 raised: | |
You have a nil object when you didn't expect it! | |
You might have expected an instance of Array. | |
The error occurred while evaluating nil.each | |
Extracted source (around line #11): | |
8: <th></th> | |
9: </tr> | |
10: | |
11: <% @answers.each do |answer| %> | |
12: <tr> | |
13: <td><%= answer.location %></td> | |
14: <td><%= link_to 'Show', answer %></td> | |
Rails.root: /Users/Richardson/rails_projects/WRLY | |
Application Trace | Framework Trace | Full Trace | |
app/views/answers/index.html.erb:11:in `_app_views_answers_index_html_erb___2623522797869996922_2159166620__2569098313933450991' | |
app/controllers/answers_controller.rb:8:in `index' | |
#======================================== | |
#views > answers > index.html.erb | |
#======================================== | |
<h1>Listing answers</h1> | |
<table> | |
<tr> | |
<th>Location</th> | |
<th></th> | |
<th></th> | |
<th></th> | |
</tr> | |
<% @answers.each do |answer| %> | |
<tr> | |
<td><%= answer.location %></td> | |
<td><%= link_to 'Show', answer %></td> | |
<td><%= link_to 'Edit', edit_answer_path(answer) %></td> | |
<td><%= link_to 'Destroy', answer, :confirm => 'Are you sure?', :method => :delete %></td> | |
</tr> | |
<% end %> | |
</table> | |
<br /> | |
<%= link_to 'New Answer', new_answer_path %> | |
#======================================== | |
# app > controllers > answers_controller.rb | |
#======================================== | |
def create | |
@micropost = Micropost.find(params[:micropost_id]) | |
@answer = @micropost.answers.create(params[:answer]) | |
respond_to do |format| | |
if @answer.save | |
format.html { redirect_to(micropost_path(@micropost), :notice => 'Answer was successfully created.') } | |
format.xml { render :xml => @micropost, :status => :created, :location => @micropost } | |
else | |
format.html { render :action => "new" } | |
format.xml { render :xml => @micropost.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
def index | |
respond_to do |format| | |
format.html # index.html.erb | |
format.xml { render :xml => @answers } | |
end | |
end | |
def show | |
@answer = Answer.find(params[:id]) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.xml { render :xml => @answer } | |
end | |
end | |
#======================================== | |
# config > route.rb | |
#======================================== | |
WRLY::Application.routes.draw do | |
resources :answers | |
resources :microposts do | |
resources :answers | |
end | |
root :to => "microposts#index" | |
end | |
#======================================== | |
# Migrations | |
#======================================== | |
class CreateMicroposts < ActiveRecord::Migration | |
def self.up | |
create_table :microposts do |t| | |
t.string :content | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :microposts | |
end | |
end | |
class CreateAnswers < ActiveRecord::Migration | |
def self.up | |
create_table :answers do |t| | |
t.string :location | |
t.integer :micropost_id | |
t.text :body | |
t.references :micropost | |
t.timestamps | |
end | |
def self.down | |
drop_table :answers | |
end | |
end | |
class AddMicopostIdToAnswer < ActiveRecord::Migration | |
def self.up | |
add_column :answers, :micropost_id, :integer | |
add_column :answers, :body, :text | |
end | |
def self.down | |
remove_column :answers, :micropost_id | |
remove_column :answers, :body | |
end | |
end | |
class AddmicropostToAnswer < ActiveRecord::Migration | |
def self.up | |
add_column :answers, :micropost, :reference | |
end | |
def self.down | |
remove_column :answers, :micropost | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment