0

我懂了ActionController::ParameterMissing (param is missing or the value is empty: order)

我的参数哈希中缺少其明确的顺序。

我的参数字典是扁平的,不包括{order=> {}}Rails 要求的。你怎么做到这一点?您如何使用 form_with 从另一个控制器提交表单,使用 rails 6?这可能吗?

文档非常糟糕。

Parameters: {"authenticity_token"=>"b1hcu3W38F+HWoi0zgcxcU/tK1mGFks8eBFAMw0ixLaJ3M3Npzceb9KGiNie9MVUxyYILHjo8bkWOvGESZ7PUA==", "cents"=>"1600.00", "amount"=>"0.82902", "side"=>"buy", "commit"=>"Buy"}

家庭控制器上的表格:

                <%= form_with(url:orders_path, model:@order, local: false) do |f| %>
                  <div class="row">
                      <div class="col-md-12">
                          <div class="form-group">
                              <label id="account_number_label">USD Amount</label><br />
                              <%= f.text_field :cents, min: 1.00, max: 3000.00, :step => 0.01, id: "buy-cents", placeholder:"$0.00", autocomplete: 'off' %>
                          </div>
                      </div>
                  </div>
                  <div class="row">
                      <div class="col-md-12">
                          <div class="form-group">
                              <label id="account_number_label">Amount</label><br />
                              <%= f.text_field :amount, min: 0.01, max: 3000.00, :step => 0.000001, id: "amount", placeholder:"0.00", autocomplete: 'off' %>
                          </div>
                      </div>
                  </div>
                  <div id="last-price" data-value=<%=@last_price %>></div>
                  <%= f.hidden_field :side, value:"buy" %>
                  <div class="row">
                      <div class="col-md-12">
                          <br>
                          <%= f.submit "Buy",  class:"btn btn-primary btn-lg btn-block submit" %>

订单控制器:

class OrdersController < ApplicationController
  before_action :authenticate_user!
  before_action :set_order, only: [:show, :destroy]
  before_action :is_resource_owner?, only: [:show, :destroy]
  def new
  end

  def create
    puts order_params
    @order = Order.create(order_params)

  end

  def show
  end

  def index
  end

  def delete
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_order
    @order = Order.find(params[:id])

  end

  def is_resource_owner?
    if current_user.id != @order.user_id
      redirect_to root_path
    end
  end

  # Only allow a list of trusted parameters through.
  def order_params
    params.require(:order).permit(:user_id, :side, :gold_troy_ounces, :cents)
  end
4

0 回答 0