我正在Rails 4 Ruby 2 中构建一个应用程序
背景:
我有一个名为"Calls"的父脚手架,然后我有一个子脚手架,名为"Respondings"。我在这里试图实现的是,通过自定义按钮点击,可以从"Calls"show.html.erb中更新"Respondings"表中的项目。(如下所示)
我的路线看起来是这样的:
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
patch:unit_responsings_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/responsibings_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/responsdings_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
所以这个问题的修复只是在routes文件中。。原来我失踪了。。在将我的路由文件更改为以下文件后,它完全解决了最初的问题。
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 %>