Спасибо, что нашли время, чтобы рассмотреть этот вопрос.
У меня есть вложенный ресурс, называемый шаблонами, которые относятся к категориям. У меня есть шаблон создания, редактирования и удаления действий, отображаемых на странице категории Показать. Я могу создать без проблем, однако, при попытке редактировать или удалить тот же шаблон получает по его идентификатору (обычно первый шаблон, созданный в категории).
Код
config / rout.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
Спасибо!