提问人:Alexander V 提问时间:1/24/2022 最后编辑:Alexander V 更新时间:1/25/2022 访问量:51
Rails,视图中嵌套对象的额外行。(更新)
Rails, extra row in a view for nested object. (updated)
问:
那里!
更新:https://github.com/czepesch/gloss - 公开了 repo。
subject:我需要删除或阻止视图(在表格中)中多出一个空行。
我的 RoR 应用程序中有两个模型:Glossary 和 Entry
class Glossary < ApplicationRecord
has_many :entries, dependent: :destroy
accepts_nested_attributes_for :entries
class Entry < ApplicationRecord
belongs_to :glossary
validates :term, presence: true, uniqueness: true
向词汇表添加条目的表单:
= form_with(model: [@glossary, @entry], local: true) do |f|
- if @entry.errors.any?
- @entry.errors.each do |error|
= error.full_message
= f.text_field :term, :placeholder => 'term'
= f.text_field :alt, :placeholder => 'alt'
= f.text_field :target, :placeholder => 'target'
= f.text_field :def, :placeholder => 'def'
= f.submit 'add'
视图/词汇表/显示中的表格(有点简化,为了便于阅读,我删除了顺风样式)
%table
%thead
%tr
%th.{:scope => 'col'} term
%th.{:scope => 'col'} alt
%th.{:scope => 'col'} target
%th.{:scope => 'col'} def
%th.{:scope => 'col'}
%span.sr-only edit
%th{:scope => 'col'}
%span.sr-only delete
%tbody
- for entry in @glossary.entries
%tr
%td= entry.term
%td= entry.alt
%td= entry.target
%td= entry.def
%td
= link_to edit_glossary_entry_path(@glossary)
%td
= link_to [@glossary, entry], data: { 'turbo_method': 'delete', turbo_confirm: "are you sure?" }
因此,任何词汇表(即使是新的,没有任何条目)的表都有一个额外的空行:额外的行
我认为这是glossaries_controller的新行动:
class GlossariesController < ApplicationController
before_action :set_glossary, only: %i[ show edit update destroy ]
def index
@glossaries = Glossary.order(params[:sort])
end
def show
@entry = @glossary.entries.new
end
def new
@glossary = Glossary.new
end
def edit
end
def create
@glossary = Glossary.new(glossary_params)
respond_to do |format|
if @glossary.save
format.html { redirect_to glossary_path(@glossary), notice: "glossary was created." }
format.json { render :show, status: :created, location: @glossary }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @glossary.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @glossary.update(glossary_params)
format.html { redirect_to glossary_path(@glossary), notice: "glossary was updated." }
format.json { render :show, status: :ok, location: @glossary }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @glossary.errors, status: :unprocessable_entity }
end
end
end
def destroy
@glossary.destroy
respond_to do |format|
format.html { redirect_to glossaries_url, notice: 'glossary was destroyed' }
format.json { head :no_content }
end
# redirect_to root_path, status: :see_other
end
private
def set_glossary
@glossary = Glossary.find(params[:id])
end
def glossary_params
params.require(:glossary).permit(:title, :description, entries_attributes: [ :term, :alt, :target, :def ])
end
end
但我无法弄清楚如何让一切以其他方式运作。
任何帮助都是值得赞赏的。
答:
0赞
Alexander V
1/25/2022
#1
def show
@entry = @glossary.entries.new
end
导致了“问题”。 我刚刚添加了
- if entry.save
在视图中,一切看起来都很好。
感谢罗克韦尔赖斯的帮助。
评论
@glossary.entries.count