2014-01-12 52 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

相關問題