编辑对象Modal Ruby on Rails

问题描述 投票:0回答:1

[Yoo,我正在尝试创建一种模式,可以用来编辑便笺。

但是当我单击编辑按钮时。它会打开模态,但是即使单击其他音符,它也只显示第一个音符的信息,它总是重定向我来编辑表格的第一个音符。

我似乎无法弄清楚。

这是我的索引视图

  <table class="table mt-5">
    <thead>
      <tr>
        <th><p>Titulo</p></th>
        <th><p>Conteudo</p></th>
        <th><p>Data</p></th>
        <th><p>Prioridade</p></th>
        <th></th>
        <th></th>
      </tr>
    </thead>

    <tbody>
      <% current_user.notes.each do |n| %>
      <tr>
        <td><%= n.title %></td>
        <td><%= n.content %></td>
        <td><%= n.date %></td>
        <td><%= n.priority %></td>
        <td> <%= link_to "Editar", edit_note_path(n), class: "btn btn-outline-warning",data: { toggle: "modal", target: "#EditNote", whatever: "@getbootstrap"} %></td>
        <td><%= link_to "Apagar", note_path(n), method: :delete, data: { confirm: 'Tem certeza ?' }, class: "btn btn-outline-danger" %>
        </td>
      </tr>
      <div class="modal fade" id="EditNote"  tabindex="-1" role="dialog" aria-labelledby="EditingNote"  aria-hidden="true">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="CreatingNote">Editar Anotação</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
            <div class="modal-body">
              <%= simple_form_for n do |f| %>
                  <%= f.input :title, label: "Title", placeholder: "Title" %>
                  <%= f.input :content, placeholder: "Escreve aqui" %>
                  <%= f.input :date %>
                  <%= f.input :priority, :collection => %w[Baixa Média Alta] %>
            </div>
            <div class="modal-footer">
                <%= f.submit "Editar", class: "btn btn-warning btn-block" %>
              <% end %>
            </div>
          </div>
        </div>
      </div>
      <% end %>
    </tbody>
  </table>

这里是我的控制器

class NotesController < ApplicationController
  before_action :set_note, only: %w[edit update destroy]

  def index
    @notes = Note.all
    @note = Note.new
  end

  def new
    @note = Note.new
  end

  def create
    @note = Note.new(note_params)
    @note.user = current_user
    if @note.save
      redirect_to notes_path
      flash[:alert] = "Anotação salva"
    else
      flash[:alert] = "Tenta de novo"
      render :new
    end
  end

  def edit
  end

  def update
    @note.update(note_params)
    redirect_to notes_path
  end

  def destroy
    @note.destroy
    redirect_to notes_path
  end

  private

  def set_note
    @note = Note.find(params[:id])
  end

  def note_params
    params.require(:note).permit(:title, :content, :date, :priority, :user_id)
  end
end
ruby-on-rails ruby bootstrap-4 bootstrap-modal
1个回答
0
投票

每个模态的ID为#EditNote。每个编辑链接都有:

data: { toggle: "modal", target: "#EditNote", whatever: "@getbootstrap"}

因此,我想单击-不管您单击哪个链接-始终显示ID为#EditNote的第一个模态,该ID始终是第一个音符的模态。

也许尝试:

data: { toggle: "modal", target: "#editNote#{n.id}", whatever: "@getbootstrap"}

...和:

<div class="modal fade" id="editNote#{n.id}"  tabindex="-1" role="dialog" aria-labelledby="EditingNote"  aria-hidden="true">

因此,每个模态都有唯一的id,每个链接都指向该唯一的id

顺便说一句,由于您没有在控制器操作中执行任何操作,因此似乎并没有必要使用link_to,但也可能不会造成任何危害。这只是一个不必要的请求-响应周期。

© www.soinside.com 2019 - 2024. All rights reserved.