感谢您抽出宝贵时间来审查此问题。
我有一个称为模板的嵌套资源,该资源属于类别。我在“类别显示”页面上显示了模板创建,编辑和删除操作。但是,当尝试编辑或删除相同模板时,我可以通过其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
<%= 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
谢谢!
感谢您抽出宝贵时间来审查此问题。我有一个称为模板的嵌套资源,该资源属于类别。我在...