Search code examples
ruby-on-railsruby-on-rails-4form-fornested-routes

Rails - nested routes and form_for


I have in routes.rb

resources :reckoners do
  resources: shift_requirements
end

In my partial for a new shift_requirement I have -

<%= form_for ([@reckoner, ShiftRequirment.new]) do |f| %>

but that's calling the path 'shift_requirement_path' which is throwing an error, though the page it's throwing an error on will be along the lines of http://localhost:3000/reckoners/2/shift_requirements/new

My routes say

reckoner_shift_requirement GET    /reckoners/:reckoner_id/shift_requirements/:id(.:format)      shift_requirements#show

etc.

How do I fix the form?

reckoner.rb has_many :shift_requirements and 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

Solution

  • Firstly, you will need to set the Reckoner for which there is a new shift_requirement. Here is how you can do it in your controller (Edited new and create action. Edited the index action to keep up with best practices :). Also added a before action and set_reckoner private method )

    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
    

    Then in your form do this ():

    <%= form_for ([@reckoner, @shift_requirement]) do |f| %>