gpt4 book ai didi

ruby-on-rails - Rails 4如何使用带有其他text_field的复选框集合来建模表单

转载 作者:行者123 更新时间:2023-12-04 07:40:15 24 4
gpt4 key购买 nike

首先让我说这也可能是一个建模问题,我愿意接受建模建议。

用例:
我有一个表格,我需要允许用户在他们的帖子类别上选择一个复选框。如果没有适合他们帖子的类别,则检查其他类别将显示一个文本字段供用户添加自定义类别。这应该用于创建和更新嵌套模块

数据库建模

class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name, null: false
t.timestamps null: false
end

reversible do |dir|
dir.up {
Category.create(name: 'Hats')
Category.create(name: 'Shirts')
Category.create(name: 'Pants')
Category.create(name: 'Shoes')
Category.create(name: 'Other')
}
end

create_table :categorizations, id: false do |t|
t.belongs_to :post, index: true, null: false
t.belongs_to :category, index: true, null: false
t.string :value
end
end
end

应用模型
class Post < ActiveRecord::Base
has_many :categorizations
accepts_nested_attributes_for :categorizations, allow_destroy: true
has_many :categories, through: :categorizations
accepts_nested_attributes_for :categories
end

class Category < ActiveRecord::Base
has_many :posts
end

Controller :
  def update

if @post.update(post_params)
flash.now[:success] = 'success'
else
flash.now[:alert] = @post.errors.full_messages.to_sentence
end

render :edit
end

private

def set_post
@post = Post.find(params[:id])
(Category.all - @post.categories).each do |category|
@post.categorizations.build(category: category)
end
@post.categorizations.to_a.sort_by! {|x| x.category.id }
end

def post_params
params.require(:post).permit(:name, :description,
categorizations_attributes: [ :category_id, :value, :_destroy],
)
end

看法:
= f.fields_for :categorizations do |ff|
= ff.check_box :_destroy, { checked: ff.object.persisted? }, '0', '1'
= ff.label :_destroy, ff.object.category.name
= ff.hidden_field :category_id
= ff.text_field :value if ff.object.category.other?

但是,使用上述解决方案,我在保存时继续遇到重复记录错误。不知道为什么会这样?有一个更好的方法吗?

最佳答案

我更喜欢这样的东西:

型号

post.rb

class Post < ActiveRecord::Base
has_many :categorizations
has_many :categories, through: :categorizations

accepts_nested_attributes_for :categorizations, allow_destroy: true
accepts_nested_attributes_for :categories
end

类别.rb
class Category < ActiveRecord::Base
has_many :categorizations
has_many :posts, through: :categorizations
end

Controller
...
def update
if @post.update(post_params)
flash.now[:success] = 'success'
else
flash.now[:alert] = @post.errors.full_messages.to_sentence
end
render :edit
end

private

def set_post
@post = Post.find(params[:id])
end

def post_params
params.require(:post).permit(:name, :description, category_ids: [])
end
...

浏览量
我总是更喜欢平原 .erb所以,
simple_form 的帮助下.
<%= simple_form_for(@post) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input :content -%>
...
</div>

<div class="form-inputs">
<%= f.association :categories, as: :check_boxes -%>
</div>

<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

您可以通过这种方式轻松干净地拥有已检查/未检查的状态并进行销毁。
此外,您可以添加
<%= f.simple_fields_for :category do |category_fields| %>
<%= category_fields.input :name -%>
<% end %>

获取关联的嵌套字段,但不要忘记将相关参数添加到 strong_params当你这样做时。
...
def post_params
params.require(:post).permit(:name, :description, category_attributes: [:name])
end
....

关于ruby-on-rails - Rails 4如何使用带有其他text_field的复选框集合来建模表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35951012/

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