1

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

路线.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.erband edit.html.erb

<%= render "form", 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

当我转到以下 urlhttp://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>

我显然错过了一些东西,但我真的不知道是什么。

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

4

1 回答 1

1

您可以将表单的 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| %>
...
于 2020-09-30T03:17:32.597 回答