【问题标题】:Rails 6 Dup in nested controller嵌套控制器中的 Rails 6 Dup
【发布时间】:2021-01-27 20:52:52
【问题描述】:

我有一个嵌套在我的站点控制器下的系统控制器。我想 .dup 一个系统,但我收到“找不到没有 ID 的系统”错误。

我的按钮是这样的

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

我的控制器

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

我的路线

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

我按照这个问题Duplicate an entry in Rails from show page 构建了我的按钮,并在非嵌套控制器上对其进行了测试,效果很好。

我想我只是在寻找如何将网站传递给组合:)

我尝试了一些东西,但似乎没有达到目标,在此先感谢。

【问题讨论】:

    标签: ruby-on-rails ruby dup


    【解决方案1】:
    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" 
    %>
    

    【讨论】:

    • Ahhh 经验教训...不要迷失在别人的解决方案中...谢谢@max
    • 没问题。你也可以在你的重复方法中去掉@site = Site.find(params[:site_id]),因为你在回调中设置了它。
    猜你喜欢
    • 2018-04-05
    • 2017-01-18
    • 1970-01-01
    • 2012-01-28
    • 2015-10-05
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多