自定义路由参数和 form_with 未使用该参数

Custom route param and form_with not using the param

我正在尝试为地址分配一个自定义 ID,这样就不容易猜到记录(对于想要添加和更改其他人地址的人)。出于某种原因,虽然我可以使用正在创建的正确路径创建记录,但来自 form_with 的编辑路径似乎没有使用自定义资源参数。

routes.rb

resources :deladdresses, param: :delid

_form.html.erb

<%= form_with model: deladdress, class: "mt-4" do |f| %>
  <%= render "shared/error_messages", resource: f.object %>

  .. lots removed for clarity ...

  <%= f.hidden_field :country, value: "Australia" %>
  <%= f.hidden_field :ordernum, name: :ordernum, value: @order %>
  <%= link_to order_path(@order.ordernum) do %>
    <button class="btn btn-secondary">Back</button>
  <% end %>
  <%= f.button "Save", id: 'submit', class: "btn btn-primary" %>
<% end %>

来自我的 new.html.erbedit.html.erb:

<%= render "form", deladdress: @deladdress %>

deladdress.rb

class Deladdress < ApplicationRecord

  before_save :add_del_id

  belongs_to :user
  has_many :orders

  def add_del_id
    randchars = ("a".."z").to_a.sample(8).join
    time = DateTime.now.strftime("%H%M%S")
    self.delid = "#{time}-#{randchars.upcase}"
  end

end

deladdress_controller.rb

class DeladdressesController < ApplicationController
  before_action :find_my_order

  def show
    # Collect the delivery address for the order
    # Do we want to collect and store these per customer?
  end

  def new
    if current_user
      @savedaddresses = Deladdress.where(user: current_user)
    end
    @deladdress = Deladdress.new
  end

  def edit
    @deladdress = Deladdress.where(delid: params[:delid]).first
  end

  def create
    @deladdress = Deladdress.create(deladdress_params)
    @deladdress.user = current_user
    if @deladdress
      @order&.update(deladdress: @deladdress)
      redirect_to order_path(@order.ordernum), notice: "Address added"
    else
      render :new
    end
  end

  def update
    @deladdress = Deladdress.where(delid: params[:delid]).first
    if @deladdress.update(deladdress_params)
      @order.update(deladdress: @deladdress)
      redirect_to order_path(@order.ordernum), notice: t(".updated")
    else
      render :edit
    end
  end

  private

  def deladdress_params
    attributes = [:first_name, :last_name, :address, :apartment, :city, :state, :country, :postcode, :ordernum, :delid]
    params.require(:deladdress).permit(*attributes)
  end

  def find_my_order
    @order = find_order
  end

end

当我转到下面url http://localhost:5000/deladdresses/112750-UBTYOJGK/edit时,我可以看到delid在那里。但是,当我查看它将尝试提交的表单时,我有以下内容。 ID 是 5 而不是 112750-UBTYOJGK。

<form class="mt-4" action="/deladdresses/5" accept-charset="UTF-8" data-remote="true" method="post">
  <input type="hidden" name="_method" value="patch">
  <input type="hidden" name="authenticity_token" value="XXXXXXXXXXXX">
  <button name="button" type="submit" id="submit" class="btn btn-primary">Save</button>
</form>

我显然遗漏了什么,但我真的不知道是什么。

在此先感谢您为完成这项工作提供的任何帮助。

您可以像这样将表单的 url 传递给表单助手:

  1. 调用编辑表单的位置:
<%= render "form", deladdress: @deladdress, url: deladress_path(@deladress.delid) %>
  1. 对于调用新表单的位置:
<%= render "form", deladdress: @deladdress, url: deladresses_path %>

然后在表格本身中:

<%= form_with model: deladdress, url: url, class: "mt-4" do |f| %>
...