如何使用确认页面更新唯一字段?

时间:2018-11-09 07:45:36

标签: ruby-on-rails ruby

因此,我有这本书的编辑页面,其中包含唯一字段(标题,ISBN)。但是在提交表单进行更新之前,我需要先将表单信息发送到另一个确认页面,然后再进行更新。但是,当我将表单信息传递到确认页面时,Title和ISBN的唯一验证失败。我有以下代码:

book.rb

  validates :title, presence: true,
                    uniqueness: true
  validates :isbn,  presence: true, 
                    uniqueness: true,
                    format: { with: /[0-9]/ }

edit.html.erb

<%= form_for @book, url: confirm_edit_admin_book_path, method: :patch, html: { class: "form" } do |f| %>
        <%= f.hidden_field :id %>
        <div class="float-right">
            <button class="btn btn-primary">Confirm</button>
        </div>
        <h4 class="card-title">Edit Book</h4>
        <div class="form-group row mt-5">
            <%= f.label :title, class: "col-md-2 col-form-label required" %>

            <div class="col-md-10">
                <%= f.text_field :title, class: "form-control" %>
                <%= render "admin/shared/error_field", field: :title %>
            </div>
        </div>

        <div class="form-group row">
            <%= f.label :isbn, "ISBN", class: "col-md-2 col-form-label required" %>

            <div class="col-md-10">
                <%= f.text_field :isbn, class: 'col-sm-12 form-control' %>
                <%= render "admin/shared/error_field", field: :isbn %>
            </div>
        </div>

        <div class="form-group row">
            <%= f.label :released_at, class: "col-md-2 col-form-label required" %>

            <div class="col-md-10">
                <%= f.date_field :released_at, class: 'col-sm-12 form-control' %>
                <%= render "admin/shared/error_field", field: :released_at %>
            </div>
        </div>

        <div class="form-group row">
            <%= f.label :description, class: "col-md-2 col-form-label" %>

            <div class="col-md-10">                       
                <%= f.text_area :description, class: 'col-sm-12 form-control' %>
                <%= render "admin/shared/error_field", field: :description %>
            </div>
        </div>

        <div class="form-group row">
            <%= f.label :quantity, class: "col-md-2 col-form-label required" %>

            <div class="col-md-10">                       
                <%= f.number_field :quantity, class: 'col-sm-12 form-control' %>
                <%= render "admin/shared/error_field", field: :quantity %>
            </div>
        </div>

        <div class="float-right">
            <%= f.submit "Confirm", class: "btn cur-p btn-primary" %>
        </div>
    <% end %>

books_controller.rb

def confirm_edit
  @book = Book.new(book_params)
  book_info = Book.find(@book.id)
  if @book.valid?
    session[:book_update] = @book
  else 
    render 'edit'
  end
end

def update
  @book = Book.find(params[:id])
  @book.update_attributes(session[:book_update].compact)
  session.delete(:book_update)
  redirect_to admin_book_url
end

1 个答案:

答案 0 :(得分:0)

confirm_edit操作中,您将使用update操作中的参数创建Book的新实例,这些参数可能是现有书籍中的参数。

@book = Book.new(book_params)

然后您将调用@book.valid?,这将失败(除非您已更改书名和ISBN),因为数据库中已经存在具有相同值的书。

您可以从数据库中检索这本书,然后使用.assign_attributes来检查有效性吗?

def confirm_edit
  @book = Book.find(params[:id])
  @book.assign_attributes(book_params)
  if @book.valid?
    session[:book_update] = @book
  else 
    render 'edit'
  end
end