Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

ruby - Rails 6 Dup in nested controller

I have a System Controller that is nested under my Site Controller. I want to .dup a system, but I am getting "Couldn't find System without an ID" error.

My Button Looks like this

<%= link_to 'Duplicate System', site_system_path(@site, item.system), action: "duplicate",  class: "dropdown-item" %>

my controller

class Sites::SystemsController < ApplicationController
  before_action :set_system, only: [:show, :edit, :update, :destroy]
  before_action :set_site

  
  def new
   @system = System.new
  end

  def duplicate
    @site = Site.find(params[:site_id])
    @system = System.find(params[:system_id]).dup
    render :new         
  end

  def create
    @system = System.new(system_params)
    @system.site = @site
      if @system.save
        render json: @system
      else
        render json: {errors: @site.errors.full_messages}
        puts @site.errors.full_messages
      end
  end

  # PATCH/PUT /systems/1
  # PATCH/PUT /systems/1.json
  def update
    respond_to do |format|
      if @system.update(system_params)
        format.html { redirect_to site_systems_path(@site), notice: 'System was successfully updated.' }
        format.json { render :show, status: :ok, location: @system }
      else
        format.html { render :edit }
        format.json { render json: @system.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /systems/1
  # DELETE /systems/1.json
  def destroy
    @system.destroy
    respond_to do |format|
      format.html { redirect_to site_systems_path(@site), notice: 'System was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_system
      @system = System.find(params[:id])
    end

    def set_site
      @site = Site.find(params[:site_id])
    end

    # Only allow a list of trusted parameters through.
    def system_params
      params.require(:system).permit(:name, :site_id, :systemtype)
    end
end

my routes

Rails.application.routes.draw do
  
  resources :sites do
    resources :systems, controller: 'sites/systems'
    get 'duplicate_system', to: 'sites/systems#duplicate'
  end

    authenticate :user, lambda { |u| u.admin? } do
      mount Sidekiq::Web => '/sidekiq'
    end


  resources :notifications, only: [:index]
  resources :announcements, only: [:index]
  devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks",
    registrations: 'users/registrations' }
  root to: 'accounts#index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

I followed this question Duplicate an entry in Rails from show page to build my button and method tested it on a non-nested controller, and it worked great.

I guess I am just looking for how I pass the site into the mix :)

I tried a few things and seemed to miss the mark, thanks in advance.

question from:https://stackoverflow.com/questions/65927242/rails-6-dup-in-nested-controller

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
resources :sites do
   resources :systems, controller: 'sites/systems' do
     get :duplicate
   end
end
<%= link_to 'Duplicate System', 
            site_system_duplicate_path(@site, item.system), 
            class: "dropdown-item" 
%>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...