Rails – 嵌套资源 – 我的编辑和删除方法检索到错误的 ID

Rails – Nested Resource - My edit and delete methods retrieve the wrong IDs

感谢您抽出宝贵时间审阅此问题。

我有一个名为模板的嵌套资源,它属于类别。我在类别显示页面中显示了模板创建、编辑和删除操作。我可以毫无问题地创建,但是,当尝试编辑或删除相同的模板时,它会通过其 ID 检索(通常是在某个类别下创建的第一个模板)。

代码

config/routes.rb

  resources :categories do
    resources :templates

app/models/template.rb

class Template < ApplicationRecord
  belongs_to :category
  validates :title, presence: true
  validates :content, presence: true
end

app/models/category.rb

class Category < ApplicationRecord
  belongs_to :user
  has_many :templates, dependent: :destroy
  validates :name, presence: true
end

db/schema.rb

create_table "categories", force: :cascade do |t|
    t.string "name"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_categories_on_user_id"
  end

  create_table "templates", force: :cascade do |t|
    t.integer "user_id"
    t.integer "category_id"
    t.string "title"
    t.text "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["category_id"], name: "index_templates_on_category_id"
    t.index ["user_id"], name: "index_templates_on_user_id"
  end

app/views/categories/show.html.erb

  <% @templates.each do |template| %>
    <div class= "template-grid">
      <div class= "template-index-card">
        <h1><%= template.title%></h1>
        <p><%= template.content %></p>
        <div class= "template-options">
          <%= link_to image_tag("edit.png", class: "icon"), edit_category_template_path([@category, @template])%>
          <%= link_to image_tag("bin.png", class: "icon"), category_template_path([@category, @template]), method: :delete,
            data: { confirm: 'Are you sure?' }%>

app/views/templates/edit.html.erb

<%= simple_form_for([@category, @template]) do |f| %>
  <div class="form-inputs">
      <%= f.input :title %>
      <%= f.input :content %>
  </div>
  <div class= "form-btn-flex">
      <%= f.button :submit, "Update Template", class:"btn-mdpm-forms"%>
    <% end %>

app/controllers/templates_controller.rb

class TemplatesController < ApplicationController

  def new
    @template = Template.new
  end

  def create
    @template = Template.new(template_params)
    @template.user_id = current_user.id
    @template.category = Category.find(params[:category_id])
    if @template.save
      redirect_to category_path(@template.category_id)
    else
      render :show
    end
  end

  def index
    @templates = Template.all
  end

  def show
    @template = Template.find(params[:id])
  end

  def edit
    @category = Category.find(params[:category_id])
    @template = @category.templates.find_by(params[:id])
  end

  def update
    @category = Category.find_by(params[:category_id])
    @template = @category.templates.find_by(params[:id])
    if @template.update(template_params)
      redirect_to category_path(@category)
    else
      render :edit
    end
  end

  def destroy
    @template = Template.find_by(params[:id])
    @template.destroy
    redirect_to category_path(@template.category_id)
  end

  private

  def template_params
    params.require(:template).permit(:title, :content)
  end
end

app/controllers/categories_controller.rb

class CategoriesController < ApplicationController
  before_action :set_category, only: [:edit, :update]

  def new
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)
    @category.user = current_user
    if @category.save
      redirect_to categories_path
    else
      render :index
    end
  end

  def index
    @category = Category.new
    @categories = Category.all
  end

  def show
    @template = Template.new
    @category = Category.find(params[:id])
    @templates = @category.templates
  end

  def edit
    @category = Category.find(params[:id])
  end

  def update
    if @category.update(category_params)
      redirect_to category_path(@category)
    else
      render :edit
    end
  end

  def destroy
    @category = Category.find(params[:id])
    @category.destroy
    redirect_to categories_path
  end

  private

  def category_params
    params.require(:category).permit(:name)
  end

  def set_category
    @category = Category.find(params[:id])
  end
end

路线

      category_templates GET    /categories/:category_id/templates(.:format)                                             templates#index
                          POST   /categories/:category_id/templates(.:format)                                             templates#create
    new_category_template GET    /categories/:category_id/templates/new(.:format)                                         templates#new
   edit_category_template GET    /categories/:category_id/templates/:id/edit(.:format)                                    templates#edit
        category_template GET    /categories/:category_id/templates/:id(.:format)                                         templates#show
                          PATCH  /categories/:category_id/templates/:id(.:format)                                         templates#update
                          PUT    /categories/:category_id/templates/:id(.:format)                                         templates#update
                          DELETE /categories/:category_id/templates/:id(.:format)                                         templates#destroy
               categories GET    /categories(.:format)                                                                    categories#index
                          POST   /categories(.:format)                                                                    categories#create
             new_category GET    /categories/new(.:format)                                                                categories#new
            edit_category GET    /categories/:id/edit(.:format)                                                           categories#edit
                 category GET    /categories/:id(.:format)                                                                categories#show
                          PATCH  /categories/:id(.:format)                                                                categories#update
                          PUT    /categories/:id(.:format)                                                                categories#update
                          DELETE /categories/:id(.:format)                                                                categories#destroy

谢谢!

如何传递参数,而不是作为数组?

<%= link_to image_tag("edit.png", class: "icon"), edit_category_template_path(category_id: @category.id, id: @template.id)%>
<%= link_to image_tag("bin.png", class: "icon"), category_template_path(category_id: @category.id, id: @template.id), method: :delete,
...

问题最终是由于太多同名变量引起的。为了解决这个问题,我必须进行以下更改:

1 - 在 app/views/categories/show.html.erb 上,我删除了 @ 表单模板,因为我在迭代中,我也没有按照建议将参数作为数组传递。

  <% @templates.each do |template| %>
    <div class= "template-grid">
      <div class= "template-index-card">
        <h1><%= template.title%></h1>
        <p><%= template.content %></p>
        <div class= "template-options">
          <%= link_to image_tag("edit.png", class: "icon"), edit_category_template_path(@category, template)%>
          <%= link_to image_tag("bin.png", class: "icon"), category_template_path(@category, template), method: :delete,
            data: { confirm: 'Are you sure?' }%>
  1. 我在 app/controllers/categories_controller.rb 的 show 方法中将 @template 变量的名称更新为 @templately 以便我可以使用 @template 在 [=22= 的编辑方法中传递 id ].