【问题标题】:Rails, extra row in a view for nested object. (updated)Rails,嵌套对象视图中的额外行。 (更新)
【发布时间】:2022-01-25 19:33:30
【问题描述】:

那里!

更新https://github.com/czepesch/gloss - 公开 repo。

主题:我需要删除或阻止视图(表中)中的一个额外的空行。

我的 RoR 应用中有两个模型:GlossaryEntry

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'

views/glossaries/show 中的表格(有点简化,为了便于阅读,我删除了顺风样式)

%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?" }

因此,任何词汇表的表(即使是新的,没有任何条目)都有一个额外的空行: extra row

我认为这与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

但我不知道如何让一切都以其他方式工作。

感谢任何帮助。

【问题讨论】:

  • 是否保存了一条没有数据的记录?当您单击垃圾桶将其删除时会发生什么?
  • 不,该行中没有数据。例如,我可以创建新的词汇表,转到它的页面和表中的条目已经有一个空行。同时,我可以通过 rails 控制台检查词汇表根本没有任何条目。单击那个垃圾桶时没有任何反应。它的网址与词汇表本身相同localhost:3000/glossaries/45
  • 这是一个令人困惑的错误。对于全新的词汇表,@glossary.entries.count 在其页面上显示什么?
  • 另外,你的 git repo 里面没有任何这些文件,只是初始提交文件,仅供参考
  • 忘了说,所有最近在 nuki-dev 分支的工作。新词汇表的条目为零。但是在glossaries_controller 的show 方法中,我使用entry = @glossary.entries.new 来使我的条目表单工作(view/entries/_form)

标签: html ruby-on-rails html-table nested iteration


【解决方案1】:
def show
    @entry = @glossary.entries.new
end

导致了“问题”。 我刚刚加了

- if entry.save

在视图中,一切看起来都很好。

感谢 Rockwell Rice 提供的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多