vendredi 31 juillet 2015

undefined method for Questions

I am getting the error undefined method `questions' for nil:NilClass when viewing different profiles. I am not seeing why this is an undefined method. Anyone have a clue what I did not do correct?

Logs:

NoMethodError (undefined method `questions' for nil:NilClass): 
app/controllers/users_controller.rb:80:in `show' 

Users Controller:

 def show
    @question = @user.questions.where("answer is not null").order("created_at DESC").page(params[:page]).per_page(3)

Questions Model:

  attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id, :twitter, :facebook
  belongs_to :user

  belongs_to :sender,:class_name => 'User',:foreign_key => 'sender_id'

  belongs_to :recipient,:class_name => 'User',:foreign_key => 'recipient_id'

  belongs_to :message

  belongs_to :conversation

  def self.total_answer
      where('answer IS NOT NULL').count
    end

    def self.total_answer_cancelled
        where('answer IS NULL').count
      end

      def self.total_male
        count_of_males = Question.joins(:sender).where(users: {gender: 'male'}).uniq.count
      end

      def self.total_female
        count_of_males = Question.joins(:sender).where(users: {gender: 'female'}).uniq.count
      end

Questions Controller:

  def show
      @question = Question.find(params[:id])
      @questions = Question.order("created_at DESC")
      respond_with(@questions)
    end

    def create
      @question = Question.new(params[:question])
      if @question.save
        @message = current_user.send_message(@question.recipient, @question.question, "You have a question from #{@current_user}") 
        @question.update(:conversation_id => @message.notification.conversation.id)
        render :json => {:notice => 'Your question was saved successfully. Thanks!' }
      else
        render :new, alert: 'Sorry. There was a problem saving your question.'
      end
    end

Aucun commentaire:

Enregistrer un commentaire