嵌套ressource的创建表单返回rails中的params错误

时间:2015-07-08 16:28:45

标签: ruby-on-rails ruby forms params

我有一张表Convocations 它有一个列日期,一个列时间和一个列subscription_id 我的convocation / new.html看起来像这样

<div class="row">
      <div class="col-xs-offset-4 col-xs-5">
        <%= form_tag subscription_convocations_path(@subscription) do %>
          <div class="form-group">
            <%= label_tag :date, "Date" %>
            <%= text_field_tag :date, nil, class: "form-control datepicker" %>
          </div>
          <div class="form-group">
            <%= label_tag :hour, "Heure de la convocation" %>
            <%= time_field_tag :hour, nil, class: 'form-control' %>
          </div>
          <%= submit_tag "Envoyer la convocation", class: "btn btn-primary" %>
        <% end %>
      </div>
    </div>

我的控制器动作聚合#create如下所示:

 def create
    @convocation = @subscription.convocations.build(convocation_params)
    authorize @convocation
    @convocation.save
    @notification = Notification.new
    @notification.user = @subscription.user
    @notification.content = "Vous êtes convoqué à #{@convocation.subscription.tournament.name} le #{@convocation.date.strftime("le %d/%m/%Y")} à #{@convocation.hour.strftime(" à %Hh%M")}"
    @notification.save
    redirect_to tournament_subscriptions_path(@subscription.tournament)
  end

最后我的convocation_params:

def convocation_params
    if current_user.judge?
      params.require(:convocation).permit(:hour, :date)
    else
      params.require(:convocation).permit(:status)
    end
  end

当我在convocation / new.html中填写表单时 - 我收到此错误:

param丢失或值为空:convocation 突出显示的代码行是def convocation_params中的代码:

params.require(:convocation).permit(:hour, :date)

这里出了什么问题?

Logs :
Started POST "/subscriptions/25/convocations" for ::1 at 2015-07-08 17:32:03 +0100
Processing by ConvocationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"cL8ESx4QRY+omoK9tuZczYzFBQZyZU81xTUQDT0kWJbNAVU2ruMF9gcxLLnBrNcNiHRjcvsjDwrM0n2lrSKJWg==", "date"=>"10/07/2015", "hour"=>"15:00", "commit"=>"Envoyer la convocation", "subscription_id"=>"25"}
  User Load (33.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]]
  Subscription Load (0.4ms)  SELECT  "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = $1 LIMIT 1  [["id", 25]]
Completed 400 Bad Request in 128ms

我使用before_action初始化@subscription:

class ConvocationsController < ApplicationController
  before_action :find_subscription, except: [:multiple_new, :multiple_create]

然后

  def find_subscription
    if params[:subscription_id] != nil
      @subscription = Subscription.find(params[:subscription_id])
    else
      @convocation = Convocation.find(params[:id])
    end

  end

我的召集/新方法:

def new     @convocation = @ subscription.convocations.build     授权@convocation   端

我与筹款相关的路线:

                  multiple_new POST     /tournaments/:tournament_id/convocations/multiple_new(.:format)    convocations#multiple_new
                multiple_create POST     /tournaments/:tournament_id/convocation/multiple_create(.:format)  convocations#multiple_create
      subscription_convocations POST     /subscriptions/:subscription_id/convocations(.:format)             convocations#create
   new_subscription_convocation GET      /subscriptions/:subscription_id/convocations/new(.:format)         convocations#new
           convocation_messages GET      /convocations/:convocation_id/messages(.:format)                   messages#index
                                POST     /convocations/:convocation_id/messages(.:format)                   messages#create
        new_convocation_message GET      /convocations/:convocation_id/messages/new(.:format)               messages#new
       edit_convocation_message GET      /convocations/:convocation_id/messages/:id/edit(.:format)          messages#edit
            convocation_message GET      /convocations/:convocation_id/messages/:id(.:format)               messages#show
                                PATCH    /convocations/:convocation_id/messages/:id(.:format)               messages#update
                                PUT      /convocations/:convocation_id/messages/:id(.:format)               messages#update
                                DELETE   /convocations/:convocation_id/messages/:id(.:format)               messages#destroy
               edit_convocation GET      /convocations/:id/edit(.:format)                                   convocations#edit
                    convocation PATCH    /convocations/:id(.:format)                                        convocations#update
                                PUT      /convocations/:id(.:format)                                        convocations#update

1 个答案:

答案 0 :(得分:1)

根据您生成的参数,这一行

params.require(:convocation).permit(:hour, :date)

应该是

params.permit(:hour, :date)

require(key) 部分所述here所述,

  

确保存在参数。如果它存在,则返回   给定键的参数,否则引发一个   ActionController :: ParameterMissing错误。

您的参数是在没有 convocation 参数的情况下生成的,错误也是如此。