ben
ben

Reputation: 6180

param not found: in rails4 strong parameters

Error while using rails 4 strong parameters.

ActionController::ParameterMissing at /questions
param not found: question

I have a Question model and questions controller. The questions table has content column

this is what is contained in the questions controller

class QuestionsController < ApplicationController
  def index
    @question = Question.new(question_params)
    @questioner = Questioner.new(questioner_params)
  end

  def new
    @question = Question.new(question_params)
  end

  def edit
    @question = find(params[:id])
    raise "Question Not edited!" unless @question
  end

  def create
    @question = Question.new(question_params)

    respond_to do |wants|
      if @question.save
        flash[:notice] = 'You have successfully posted the questions!'
        wants.html { redirect_to(root_path) }
        wants.xml  { render :xml => @question, :status => :created, :location => @question }
      else
        flash[:error] = "Please review the problems below."
        wants.html { redirect_to(questions_path) }
        wants.xml  { render :xml => @question.errors, :status => :unprocessable_entity }
      end
    end
  end

  private

    def question_params
      params.require(:question).permit(:content)
    end
end

Upvotes: 0

Views: 621

Answers (3)

Hass
Hass

Reputation: 1636

You are using questioner_params but it's not defined anywhere. Besides, when you're displaying the index action, you're not setting any params. You only need to input the params when the user click submit which should go to the create action.

def index
  @question = Question.new
  @questioner = Questioner.new
end

Upvotes: 2

David Williams
David Williams

Reputation: 1

Where is def show ? It seems very important in the questions controller.

try

def show
@question = Question.find(params[:id])
end

Upvotes: -1

dax
dax

Reputation: 10997

The problem is in your controller's index action:

def index
  @question = Question.new(question_params)
  @questioner = Questioner.new(questioner_params)
end

First, there's no questioner_params. More importantly, there are no params for the index action - look at the output of rake routes Try defining @questions = Question.all instead of @question = Question.new for the index action since index is used to show the collection of all questions.

Check this out, too, it's a good read and really helpful when you're just beginning:

http://guides.rubyonrails.org/getting_started.html

Upvotes: 0

Related Questions