我有routes.rb
resources :reckoners do
resources: shift_requirements
end
在我支持新的shift_requirement时,我有 -
<%= form_for ([@reckoner, ShiftRequirment.new]) do |f| %>
但是这会调用路径'shift_requirement_path'而引发错误,尽管它引发错误的页面将是http://localhost:3000/reckoners/2/shift_requirements/new
我的路线说
reckoner_shift_requirement GET /reckoners/:reckoner_id/shift_requirements/:id(.:format) shift_requirements#show
等
如何修复表单?
reckoner.rb has_many :shift_requirements
和shift_reqiurement.rb belongs_to:reckoner
shift_requirements_controller.rb -
class ShiftRequirementsController < ApplicationController
before_action :set_shift_requirement, only: [:show, :edit, :update, :destroy]
# GET /shift_requirements
# GET /shift_requirements.json
def index
@shift_requirements = ShiftRequirement.where(reckoner_id: params[:reckoner_id])
@shift_requirement = ShiftRequirement.new
end
# GET /shift_requirements/1
# GET /shift_requirements/1.json
def show
end
# GET /shift_requirements/new
def new
@shift_requirement = ShiftRequirement.new
end
# GET /shift_requirements/1/edit
def edit
end
def create
@shift_requirement = ShiftRequirement.new(shift_requirement_params)
zero_ise @shift_requirement
totalise_FTEs @shift_requirement
respond_to do |format|
if @shift_requirement.save
format.html { redirect_to shift_requirements_path(reckoner_id:@shift_requirement.reckoner_id), notice: 'Shift requirement was successfully created.' }
format.json { render :show, status: :created, location: @shift_requirement }
else
format.html { render :new }
format.json { render json: @shift_requirement.errors, status: :unprocessable_entity }
end
end
end
def update
totalise_FTEs @shift_requirement
respond_to do |format|
if @shift_requirement.update(shift_requirement_params)
format.html { redirect_to shift_requirements_path(reckoner_id: @shift_requirement.reckoner_id), notice: 'Shift requirement was successfully updated.' }
format.json { render :show, status: :ok, location: @shift_requirement }
else
format.html { render :edit }
format.json { render json: @shift_requirement.errors, status: :unprocessable_entity }
end
end
end
def destroy
@shift_requirement.destroy
respond_to do |format|
format.html { redirect_to shift_requirements_url, notice: 'Shift requirement was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_shift_requirement
@shift_requirement = ShiftRequirement.find(params[:id])
end
def shift_requirement_params
params.require(:shift_requirement).permit(:shift, :hours, :mon_count, :tue_count, :wed_count, :thu_count, :fri_count, :sat_count, :sun_count, :TotalFTE, :reckoner_id)
end
def zero_ise(shift)
shift.mon_count ||= 0.0
shift.tue_count ||= 0.0
shift.wed_count ||= 0.0
shift.thu_count ||= 0.0
shift.fri_count ||= 0.0
shift.sat_count ||= 0.0
shift.sun_count ||= 0.0
end
def totalise_FTEs(shift)
zero_ise shift
shift.TotalFTE = (shift.mon_count + shift.tue_count + shift.wed_count + shift.thu_count + shift.fri_count + shift.sat_count + shift.sun_count)*shift.hours/Reckoner.find_by_id(shift.reckoner_id).weekly_hours
end
end
答案 0 :(得分:2)
首先,您需要设置有新shift_requirement的Reckoner。以下是如何在控制器中执行此操作(编辑新的和创建操作。编辑索引操作以跟上最佳实践:)。还添加了一个before action和set_reckoner私有方法)
class ShiftRequirementsController < ApplicationController
before_action :set_shift_requirement, only: [:show, :edit, :update, :destroy]
before_action :set_reckoner
# GET /shift_requirements
# GET /shift_requirements.json
def index
@shift_requirements = @reckoner.shift_requirements.all
end
# GET /shift_requirements/1
# GET /shift_requirements/1.json
def show
end
# GET /shift_requirements/new
def new
@shift_requirement = @reckoner.shift_requirements.new
end
# GET /shift_requirements/1/edit
def edit
end
def create
@shift_requirement = @reckoner.shift_requirements.new(shift_requirement_params)
zero_ise @shift_requirement
totalise_FTEs @shift_requirement
respond_to do |format|
if @shift_requirement.save
format.html { redirect_to shift_requirements_path(reckoner_id:@shift_requirement.reckoner_id), notice: 'Shift requirement was successfully created.' }
format.json { render :show, status: :created, location: @shift_requirement }
else
format.html { render :new }
format.json { render json: @shift_requirement.errors, status: :unprocessable_entity }
end
end
end
def update
totalise_FTEs @shift_requirement
respond_to do |format|
if @shift_requirement.update(shift_requirement_params)
format.html { redirect_to shift_requirements_path(reckoner_id: @shift_requirement.reckoner_id), notice: 'Shift requirement was successfully updated.' }
format.json { render :show, status: :ok, location: @shift_requirement }
else
format.html { render :edit }
format.json { render json: @shift_requirement.errors, status: :unprocessable_entity }
end
end
end
def destroy
@shift_requirement.destroy
respond_to do |format|
format.html { redirect_to shift_requirements_url, notice: 'Shift requirement was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_shift_requirement
@shift_requirement = ShiftRequirement.find(params[:id])
end
def set_reckoner
@reckoner = Reckoner.find(params[:reckoner_id])
end
def shift_requirement_params
params.require(:shift_requirement).permit(:shift, :hours, :mon_count, :tue_count, :wed_count, :thu_count, :fri_count, :sat_count, :sun_count, :TotalFTE, :reckoner_id)
end
def zero_ise(shift)
shift.mon_count ||= 0.0
shift.tue_count ||= 0.0
shift.wed_count ||= 0.0
shift.thu_count ||= 0.0
shift.fri_count ||= 0.0
shift.sat_count ||= 0.0
shift.sun_count ||= 0.0
end
def totalise_FTEs(shift)
zero_ise shift
shift.TotalFTE = (shift.mon_count + shift.tue_count + shift.wed_count + shift.thu_count + shift.fri_count + shift.sat_count + shift.sun_count)*shift.hours/Reckoner.find_by_id(shift.reckoner_id).weekly_hours
end
end
然后在你的表单中执行此操作():
<%= form_for ([@reckoner, @shift_requirement]) do |f| %>