On 6 April 2012 20:25, Vikram Anand <[email protected]> wrote:
> For some reason (most likely in my view due to naming conventions) rails
> association is not working. Trying to access child object from parent object
> gives undefined method.
>
> ...
> Sns::Application.routes.draw do
> root :to => "questions#index"
> resources :questions do
> resources :question_responses
> end
>
>
> question.rb:
>
> class Question < ActiveRecord::Base
> validates :title, :presence => true
> validates :description, :presence => true
>
> has_many :question_responses, :dependent => :destroy, :class_name =>
> "QuestionResponse"
> accepts_nested_attributes_for :question_responses, :allow_destroy => true
> end
>
>
> question_response.rb:
>
> class QuestionResponse < ActiveRecord::Base
> validates :body, :presence => true
> belongs_to :question, :foreign_key => "question_id"
> end
>
>
> questions_controller.rb:
>
> class QuestionsController < ApplicationController
> before_filter :find_question
> before_filter :find_question_response, :only => [:show, :edit, :update,
> :destroy]
>
> def index
> @question = Question.new
> @questions = Question.all
> end
>
> def new
> @question = Question.new
> @question_response = @question.question_responses.build
> end
>
> def create
> @question = Question.new(params[:question])
>
> if @question.save
> flash[:notice] = "Question has been created."
> redirect_to @question
> else
> flash[:alert] = "Question has not been created."
> render :action => 'new'
> end
>
> @question_response =
> @question.question_responses.build(params[:question_response])
> if @question_response.save
> flash[:notice] = "Reply has been created."
> redirect_to [@question, @question_response]
> else
> flash[:alert] = "Reply has not been created."
> render "question_responses/new"
> end
> end
>
> def show
> @question = Question.find(params[:id])
> end
>
> def edit
> @question = Question.find(params[:id])
> end
>
> def update
> @question = Question.find(params[:id])
>
> if @question.update_attributes(params[:question])
> flash[:notice] = "Question has been updated."
> redirect_to @question
> else
> flash[:alert] = "Question has not been updated."
> render :action => 'edit'
> end
> end
>
> def destroy
> @question = Question.find(params[:id])
> @question.destroy
> flash[:notice] = "Question has been deleted."
> redirect_to questions_path
> end
>
> def find_question_response
> @question_response = @question.question_responses.find(params[:id])
> end
>
> private
> def find_question
> if (params[:question_response] &&
> params[:question_response][:question_id])
> @question = Question.find(params[:question_response][:question_id])
> elsif params[:question_id]
> @question = Question.find(params[:question_id])
> end
> end
> end
>
>
> Error with trace:
>
> undefined method `question_responses' for nil:NilClass
>
> app/controllers/questions_controller.rb:55:in `find_question_response'
Read the error carefully, it says that nil has not got a method
question_responses, so the object you are trying to call the method on
is nil. So apparently @question is nil, assuming that line 55 is
@question_response = @question.question_responses.find(params[:id])
The reason is that this is being called in a before_filter and you
have not got any code there to setup @question.
Colin
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.