如何修复Rails中的“ ActionController :: ParameterMissing”错误?

时间:2019-05-25 03:57:29

标签: ruby-on-rails

我正在创建一个测验应用程序,并且希望该表单将用户的答案发送到控制器,但无法克服此错误。

我读过的大多数答案似乎都指出.permit和.require方法是罪魁祸首,所以我尝试了所有可能的排列方式。

我认为该表单:

<%= form_tag("/cardstates/#{@nextcard.id}/update", method: "post") do %>
  <%= text_field_tag("answer[first]") %>
  <%= submit_tag("Submit Answer") %>
<% end %>

我的控制器:

class CardstatesController < ApplicationController

    def update
        @nextcard = Cardstate.find(params[:id]) 
        @user = @nextcard.user
        if @nextcard.update(cardstate_params)
            redirect_to study_path(@user)
        end
    end

    private

    def cardstate_params
        params.require(:answer).permit(:first)
    end

end

参数:

{"utf8"=>"✓", "authenticity_token"=>"xNTQj4C4NkR0KhghqKwia/Kb0K7MI/eOWfQMfpUFXNkpK7QP560SqAKAtOqaqk4kIWAOKAKSewewL1ptGvPS+A==", "answer"=>{"first"=>"222"}, "commit"=>"Submit Answer", "id"=>"68"}

routes.rb中的相关行

post 'cardstates/:id/update', to: 'cardstates#update'

我希望能够首先调用答案(高于222),然后我可以根据答案执行必要的逻辑。

1 个答案:

答案 0 :(得分:0)

先转换

post 'cardstates/:id/update', to: 'cardstates#update'

put 'cardstates/:id/update', to: 'cardstates#update'

另一个

<%= form_tag("/cardstates/#{@nextcard.id}/update", method: "post") do %>

<%= form_tag("/cardstates/#{@nextcard.id}/update", method: "put") do %>

,在控制器中,您只需调用cardstate_params[:answer],您就会得到222