我正在构建一个在线个人图书馆应用程序,我想拥有它,以便用户可以添加书籍到他们的图书馆。我想检查一下,添加到库中的书是否已经存在于数据库中,然后将该实例添加到该库中。现在,我的表单被设置成总是创建一本书的新实例,而不检查这本书是否存在。我在rails还是个新手,如果有什么帮助我会很感激的。这是我的代码:
模型
库.rb:

class Library < ActiveRecord::Base
  belongs_to :user
  has_many :shelves, dependent: :destroy

  has_many :catalogs, dependent: :destroy
  has_many :books, :through => :catalogs, dependent: :destroy

  validates :user_id, presence: true
  validates :name, presence: true, length: { maximum: 100 }
  ...
end

图书.rb:
class Book < ActiveRecord::Base
  has_many :catalogs, dependent: :destroy
  has_many :libraries, :through => :catalogs, dependent: :destroy

  has_many :bookshelves, dependent: :destroy
  has_many :shelves, :through => :bookshelves, dependent: :destroy

  validates :title, presence: true, length: { maximum: 140 }
  validates :author, presence: true, length: { maximum: 140 }
  validates :publisher, presence: true, length: { maximum: 140 }
  validates :isbn, presence: true, uniqueness: true
  ...
end

目录.rb:
class Catalog < ActiveRecord::Base
  belongs_to :book
  belongs_to :library
end

控制器
图书控制器.rb
class BooksController < ApplicationController
  ...

  def create
    @book = Book.new(book_params)
    @library = current_user.library

    if @book.save
      @book.catalogs.create(:library_id => @library.id)
      flash[:success] = "Book added to library!"
      redirect_to current_user
    else
      render 'current_user'
    end
  end

  ...

private
  def book_params
    params.require(:book).permit(:title, :author, :publisher, :isbn, shelf_ids: [])
  end
  ...
end

查看
_ book_form.html.erb(图书格式):
<%= form_for(@book) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :title, placeholder: "Book Title" %>
    <%= f.text_field :author, placeholder: "Author" %>
    <%= f.text_field :publisher, placeholder: "Publisher" %>
    <%= f.text_field :isbn, placeholder: "ISBN" %>

    <%= f.fields_for :catalogs do |ff| %>
      <%= ff.hidden_field :library_id %>
    <% end %>

    <%= f.collection_select :shelf_ids, current_user.library.shelves.all, :id, :name, {:selected => @book.shelf_ids, :include_blank => true}, {:multiple => true} %>
  </div>
  <%= f.submit "Add Book to Library", class: "btn btn-primary" %>
<% end %>

最佳答案

这听起来很适合find_or_initialize_by
在这种情况下,你会在你的模型中这样做。因此,如果它找到一个存在,它将返回该实例,如果不是,它将返回一个新实例(在您的控制器中):
编辑,您可能需要手动传入book参数(以避免表单中的其他参数)。也经过编辑以反映书架。

 @book = Book.find_or_initialize_by(name: params[:name], author:  params[:author], publisher: params[:publisher], isbn: params[:isbn]])
 shelfs = Shelf.find(params[:shelf_ids])
 @book.shelfs = shelfs
 @book.save

当谈到shelfs时,可能有许多其他方法可以工作,但是这将根据参数找到它们的所有实例,然后将它们分配给这个book实例。

关于ruby-on-rails - Rails 4:提交表单时要检查对象是否已经存在,然后使用其他信息(多对多关系)更新该记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29654608/

10-11 03:01
查看更多