gpt4 book ai didi

ruby-on-rails - rails 4 : Want to check if an object already exists when submitting a form and then update that record with additional info (many-to-many relationship)

转载 作者:搜寻专家 更新时间:2023-10-30 23:45:15 24 4
gpt4 key购买 nike

我正在构建一个在线个人图书馆应用程序,我希望拥有它以便用户可以将书籍添加到他们的图书馆。我想检查并查看要添加到图书馆的图书是否已存在于数据库中,然后将该图书的实例添加到该图书馆中。现在,我的表单设置为始终创建一本书的新实例,并且不检查该书是否存在。我在 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

Controller

books_controller.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 .

在那种情况下,您将在您的模型中执行此操作。因此,如果它找到一个存在的实例,它将返回该实例,如果不存在,它将返回一个新实例(在您的 Controller 中):

编辑,您可能需要手动传递您的 book 参数(以避免表单中的其他参数)。还编辑以反射(reflect)货架。

 @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 时,可能还有许多其他方法可以工作,但这会根据参数找到它们的所有实例,然后将它们分配给这本 实例。

关于ruby-on-rails - rails 4 : Want to check if an object already exists when submitting a form and then update that record with additional info (many-to-many relationship),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29654608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com