从父显示页面更新嵌套资源的问题

时间:2015-11-29 21:43:51

标签: ruby postgresql ruby-on-rails-4

我在Rails 4 Ruby 2中构建应用程序

  

背景

我有一个名为“Calls”的父脚手架,然后我有一个名为“Respondings”的儿童脚手架。我在这里尝试实现的是能够通过自定义按钮单击从“调用”show.html.erb更新“响应”表中的项目。 (如下所示)

  

我的路线如下:

resources :calls do
    resources :respondings, except: [:index], controller: 'calls/respondings' do
      member do
        patch :unit_responding_update
      end
    end
    resources :pings, except: [:index], controller: 'calls/pings'
    resources :agencies, except: [:index], controller: 'calls/agencies'
    resources :incidents, except: [:index], controller: 'calls/incidents'
    resources :complainants, except: [:index], controller: 'calls/complainants'
  end
end

补丁:unit_respondings_update是按下时更新表的自定义方法。

  

自定义Def Rake输出在此处:

unit_responding_update_call_responding PATCH  /calls/:call_id/respondings/:id/unit_responding_update(.:format) calls/respondings#unit_responding_update

我的问题是我试图将它放在calls_controller.rb和calls / responds_controller.rb中都无济于事。下面列出的是整个两个控制器。我遇到的另一个大问题是在按钮中生成路径以读取并执行该功能。

  

父控制器“Calls_controller”

class CallsController < ApplicationController
  before_action :set_call, only: [:show, :edit, :update, :destroy]

  # GET /calls
  # GET /calls.json
  def index   
    @calls = Call.all
    @active_calls = @calls.select{ |x| x.status == 'ACTIVE' }
    @pending_calls = @calls.select{ |x| x.status == 'PENDING'}
    @clear_calls = @calls.select{ |x| x.status == 'CLEAR'}
  end

  # GET /calls/1
  # GET /calls/1.json
  def show
    @call = Call.find(params[:id])
    ## Responding Nested
    @respondings = @call.respondings
    ## Ping Nested
    @pings = @call.pings
    ## Agency Nested
    @agencies = @call.agencies
    ## Incidents Nested
    @incidents = @call.incidents
    ## Complainants Nested
    @complainants = @call.complainants
  end

  # GET /calls/new
  def new
    @call = Call.new
  end

  # GET /calls/1/edit
  def edit
  end

  # POST /calls
  # POST /calls.json
  def create
    @call = Call.new(call_params)
    @call.status = "PENDING"
    respond_to do |format|
      if @call.save
        format.html { redirect_to @call, notice: 'Call was successfully created.' }
        format.json { render :show, status: :created, location: @call }
      else
        format.html { render :new }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /calls/1
  # PATCH/PUT /calls/1.json
  def update
    respond_to do |format|
      if @call.update(call_params)
        format.html { redirect_to @call, notice: 'Call was successfully updated.' }
        format.json { render :show, status: :ok, location: @call }
      else
        format.html { render :edit }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /calls/1
  # DELETE /calls/1.json
  def destroy
    @call.destroy
    respond_to do |format|
      format.html { redirect_to calls_url, notice: 'Call was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def call_params
      params.require(:call).permit(:site_id, :call_number, :call_type, :call_type_other, :call_details, :user_id, :status)
    end
end
  

子控制器“calls / responds_controller”:当前找到自定义方法的地方:

class Calls::RespondingsController < ApplicationController
  #before_action :set_responding, only: [:show, :edit, :update, :destroy]

  # GET /respondings
  # GET /respondings.json
  def index
    @respondings = Responding.all
  end

  # GET /respondings/1
  # GET /respondings/1.json
  def show
  end

  # GET /respondings/new
  def new
    @call = Call.find(params[:call_id])
    @responding = Responding.new
  end

  # GET /respondings/1/edit
  def edit
  end

  # POST /respondings
  # POST /respondings.json
  def create
    @call = Call.find(params[:call_id])
    @responding = Responding.new(responding_params)
    @responding.call = @call

    respond_to do |format|
      if @responding.save
        format.html { redirect_to @call, notice: 'Responding was successfully created.' }
        format.json { render :show, status: :created, location: @call }
      else
        format.html { render :new }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /respondings/1
  # PATCH/PUT /respondings/1.json
  def update
    respond_to do |format|
      if @responding.update(responding_params)
        format.html { redirect_to @responding, notice: 'Responding was successfully updated.' }
        format.json { render :show, status: :ok, location: @responding }
      else
        format.html { render :edit }
        format.json { render json: @responding.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /respondings/1
  # DELETE /respondings/1.json
  def destroy
    @call = Call.find(params[:call_id])
    @responding = Responding.find(params[:id])

    if @responding.destroy
      flash[:notice] = "Successfully removed unit from call."
      redirect_to @call
    else
      flash[:error] = "There was an error removing this responder from this call."
    end 
  end

  #Custom Controller Calls
  def unit_responding_update
    @call = Call.find(params[:call_id])
    @responding = Responding.find(params[:id])
    @responding.responding_tme = DateTime.now
    @responding.responding = "true"
    @responding.on_scene = "false"
    @responding.clear = "false"
    @responding.status = "RESPONDING"
    @responding.save!
      respond_to do |format|
      if @responding.save
        format.html { redirect_to @call, notice: "Responding time successfully updated. Your status - RESPONDING" }
      else
        format.html { render action: 'edit' }
      end
    end
  end


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

    # Never trust parameters from the scary internet, only allow the white list through.
    def responding_params
      params.require(:responding).permit(:call_id, :user_id, :responding_tme, :on_scene_tme, :clear_tme, :responding, :on_scene, :clear, :status)
    end
end

如果您需要更多信息,请不要犹豫!我一直处于停滞状态,直到这个问题得到解决! (或访问我的Git,其中存储完整的上下文)

先谢谢大家!

  

编辑#1: - 添加呼叫/响应模型关系

呼叫模型关系

class Call < ActiveRecord::Base
  has_many :respondings, dependent: :destroy
end

响应的模型关系

class Responding < ActiveRecord::Base
  has_many :incidents
  belongs_to :call 
end

1 个答案:

答案 0 :(得分:0)

所以修复这个问题在路径文件中是soley ..事实证明我失踪并结束..在将路线文件更改为以下后,它完全解决了初始问题。

  resources :calls do
    resources :respondings, except: [:index], controller: 'calls/respondings' do
      member do
        patch :unit_responding_update
      end
    end
    resources :pings, except: [:index], controller: 'calls/pings'
    resources :agencies, except: [:index], controller: 'calls/agencies'
    resources :incidents, except: [:index], controller: 'calls/incidents'
    resources :complainants, except: [:index], controller: 'calls/complainants'
  end
end

路线修复后,我使用了以下链接:

<%= link_to "Responding", unit_responding_update_call_responding_path(call_id: @call.id, id: @respondings.first.id), method: :patch %>