Ruby on rails 轨道&x2013;嵌套资源-我的编辑和删除方法检索错误的ID

Ruby on rails 轨道&x2013;嵌套资源-我的编辑和删除方法检索错误的ID,ruby-on-rails,ruby,simple-form,nested-resources,Ruby On Rails,Ruby,Simple Form,Nested Resources,感谢您抽出时间回顾此问题 我有一个名为templates的嵌套资源,它属于类别。我在类别显示页面中显示了模板创建、编辑和删除操作。但是,当试图编辑或删除同一模板时,我可以通过其ID(通常是在类别下创建的第一个模板)检索该模板,而不会产生任何问题 代码 config/routes.rb resources :categories do resources :templates app/models/template.rb class Template < Application

感谢您抽出时间回顾此问题

我有一个名为templates的嵌套资源,它属于类别。我在类别显示页面中显示了模板创建、编辑和删除操作。但是,当试图编辑或删除同一模板时,我可以通过其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/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?' }%>

谢谢

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

<%= 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上,我删除了@form模板,因为我在迭代中,我还传递了参数,而不是建议的数组

  <% @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/controllers/categories\u controller.rb的show方法中将@template变量的名称更新为@template,这样我就可以使用@template在template\u控制器的edit方法上传递id

  • 由于链接位于categories/show视图中,因此show us categories\u controllerI添加了categories\u Controllers。谢谢谢谢我以前试过,但不幸的是,它不起作用。我得到以下错误:没有路由匹配{:action=>“edit”,:category_id=>“1”,:controller=>“templates”,:id=>nil},缺少必需的键:[:id]
    <%= 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 %>
    
    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
    
    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,
    ...
    
      <% @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?' }%>