2014-01-12 21 views
0

使用rails 4强参数时出错。未找到参数:在rails4强参数中

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

我有一个Question modelquestions controller。这些问题表有content column

这是包含在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 

回答

0

问题是在您的控制器的索引操作:

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

首先,有没有questioner_params。更重要的是,索引操作没有参数 - 查看rake routes的输出尝试定义@questions = Question.all而不是@question = Question.new作为索引操作,因为索引用于显示所有问题的集合。

检查了这一点,太,这是一个良好的阅读,当你刚开始确实有所帮助:

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

我在索引页中有一个2表单。一个使用问题和另一个提问者 – ben

-1

哪里是高清显示?这在问题控制器中似乎非常重要。

尝试

高清显示
@question = Question.find(PARAMS [:编号])
结束

+0

David我很欣赏答案,但显示操作仅用于显示单个问题的更多详细信息。 – ben

2

您正在使用questioner_params,但它没有任何地方所定义。此外,当您显示索引操作时,您不会设置任何参数。你只需要输入参数,当用户点击提交应该去创建行动。

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

谢谢,它帮助 – ben

+0

,但现在我得到了未定义的方法'问题'错误。 PLZ检查http://stackoverflow.com/questions/21101512/undefined-method-model-name-question-for-rails-4,并尽可能帮助。 – ben

相关问题