Ruby on Rails - 参数数量错误(0表示1)

时间:2015-08-29 12:25:27

标签: ruby-on-rails

我创建了一个脚手架模板并收到以下错误:

  

DuelsController中的ArgumentError#创建错误的参数数量(0   1)

提取的来源(第74行):

# Never trust parameters from the scary internet, only allow the white list through.
def duel_params
  params.require[:duel].permit(:euro, :authenticity_token)
end
end

参数是:

{"utf8"=>"✓",
 "authenticity_token"=>"SxksBvZNjPuciScahht76K2fj8r3AWEGe0MGmPfJUfF84GKy8Z2dK8dMGRBRiQ4L1paHUKpdTs6YxUjt6K3nWA==",
 "duel"=>{"euro"=>"5"},
 "commit"=>"Create Duel"}

代码 控制器

def create
@duel = Duel.new(duel_params)

respond_to do |format|
  if @duel.save
    format.html { redirect_to @duel, notice: 'Duel was successfully created.' }
    format.json { render :show, status: :created, location: @duel }
  else
    format.html { render :new }
    format.json { render json: @duel.errors, status: :unprocessable_entity }
  end
end

# Never trust parameters from the scary internet, only allow the white list through.
def duel_params
  params.require[:duel].permit(:euro, :authenticity_token)
end

表格

 <h1>New Duel</h1>

<%= simple_form_for @duel do |d| %>
  <%= d.input :euro %>
  <%= d.button :submit %>
<% end %>

<%= link_to 'Back', duels_path %>

错误在哪里? :/

1 个答案:

答案 0 :(得分:2)

您应该将duel_params更改为

def duel_params
  params.require(:duel).permit(:euro)
end