【发布时间】:2015-11-30 09:16:44
【问题描述】:
我正在用 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/respondings_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/respondings_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
【问题讨论】:
标签: ruby postgresql ruby-on-rails-4