1

I am working on a Rails 4 App, one of my modules is a general patrol report in which a user creates a report based on his / her shift shift start / stop. after the report is created they have the option to go back into the report view and add a patrol report.

Every thing is working up to the gen_rep_ent form submission.

The specific error I get is:

 undefined method `general_report=' for #<GenRepEnt:0x007f871d9ea150>

followed by:

  def create              
    @general_report = GeneralReport.find(params[:general_report_id])
    @gen_rep_ent = GenRepEnt.new(gen_rep_ent_params)
    @gen_rep_ent.general_report = @gen_rep_ent <-- Problem Line

    respond_to do |format|
      if @gen_rep_ent.save
        format.html { redirect_to @general_report, notice: 'General Report Entry was successfully created.' }
        format.json { render :show, status: :created, location: @general_report}
      else
        format.html { render :new }
        format.json { render json: @general_report.errors, status: :unprocessable_entity }
      end
    end
  end

It is the 3rd line that is causing the issue here.

My general_report model has the following relationship:

has_many :gen_rep_ents, dependent: :destroy

My Routes File looks Like:

Rails.application.routes.draw do

  resources :mobile_alarm_reports

  resources :mobile_incident_reports

  resources :static_incident_reports

  resources :general_reports do 
    resources :gen_rep_ents, except: [:index], controller: 'general_reports/gen_rep_ents'
  end

  resources :visitor_parkings

  resources :residents

  resources :sites

  devise_for :users, controllers: { registrations: "registrations" }

  # Adds Static Pages 
  root 'home#index'

  get 'home/about'

  get 'home/contact'

  get 'home/pricing'

My show file looks like:

<% @gen_rep_ents.each do |gen_rep_ent| %>
  <table>
    <thead>
       <tr>
        <th>Time</th>
        <th>Report</th>
      </tr>     
    </thead>
    <tbody>
      <tr>
        <td><%= gen_rep_ent.time %></td>
        <td><%= gen_rep_ent.report %></td>
      </tr>
    </tbody>
    <% end %>
  </table>

gen_rep_ents_controller.rb: <-- The nested Item

class GeneralReports::GenRepEntsController < ApplicationController
  before_action :set_gen_rep_ent, only: [:show, :edit, :update, :destroy]

  # GET /gen_rep_ents
  # GET /gen_rep_ents.json
  def index
    @gen_rep_ents = GenRepEnt.all
  end

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

  # GET /gen_rep_ents/new
  def new
    @general_report = GeneralReport.find(params[:general_report_id])
    @gen_rep_ent = GenRepEnt.new
  end

  # GET /gen_rep_ents/1/edit
  def edit
  end

  # POST /gen_rep_ents
  # POST /gen_rep_ents.json
  def create
    @general_report = GeneralReport.find(params[:general_report_id])
    @gen_rep_ent = GenRepEnt.new(gen_rep_ent_params)
    @gen_rep_ent.general_report = @gen_rep_ent

    respond_to do |format|
      if @gen_rep_ent.save
        format.html { redirect_to @general_report, notice: 'General Report Entry was successfully created.' }
        format.json { render :show, status: :created, location: @general_report}
      else
        format.html { render :new }
        format.json { render json: @general_report.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /gen_rep_ents/1
  # PATCH/PUT /gen_rep_ents/1.json
  def update
    respond_to do |format|
      if @gen_rep_ent.update(gen_rep_ent_params)
        format.html { redirect_to @gen_rep_ent, notice: 'General Report Entry was successfully updated.' }
        format.json { render :show, status: :ok, location: @gen_rep_ent }
      else
        format.html { render :edit }
        format.json { render json: @gen_rep_ent.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /gen_rep_ents/1
  # DELETE /gen_rep_ents/1.json
  def destroy
    @gen_rep_ent.destroy
    respond_to do |format|
      format.html { redirect_to gen_rep_ents_url, notice: 'General Report Entry was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def gen_rep_ent_params
      params.require(:gen_rep_ent).permit(:time, :report, :general_report)
    end
end

The general_reports_controller: <-- Parent Item

class GeneralReportsController < ApplicationController
  before_action :set_general_report, only: [:show, :edit, :update, :destroy]

  # GET /general_reports
  # GET /general_reports.json
  def index
    @general_reports = GeneralReport.all
  end

  # GET /general_reports/1
  # GET /general_reports/1.json
  def show
    @general_report = GeneralReport.find(params[:id])
    @gen_rep_ents = @general_report.gen_rep_ents
  end

  # GET /general_reports/new
  def new
    @general_report = GeneralReport.new
  end

  # GET /general_reports/1/edit
  def edit
  end

  # POST /general_reports
  # POST /general_reports.json
  def create
    @general_report = GeneralReport.new(general_report_params)

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

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

  # DELETE /general_reports/1
  # DELETE /general_reports/1.json
  def destroy
    @general_report.destroy
    respond_to do |format|
      format.html { redirect_to general_reports_url, notice: 'General report was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def general_report_params
      params.require(:general_report).permit(:user_id, :site_id, :date, :shift_start, :shift_end, :gp_number)
    end
end

I am not too sure where I'm going wrong with this as I have reviewed the tutorial over and over, and it all matches from what I can see.

EDIT # 1

my GenRepEnt model looks like:

class GenRepEnt < ActiveRecord::Base

  belongs_to :general_report

end

EDIT # 2

After performing the migration and adding belongs_to :general_report to the model I get the following error:

ActiveRecord::AssociationTypeMismatch in GeneralReports::GenRepEntsController#create

5
  • What does the GenRepEnt model look like? Commented Sep 10, 2015 at 1:19
  • Blanks with nothing in it. Commented Sep 10, 2015 at 1:23
  • Was told the has many would make it all work Commented Sep 10, 2015 at 1:23
  • 1
    Change this line @gen_rep_ent.general_report = @gen_rep_ent to @gen_rep_ent.general_report_id = @general_report.id Commented Sep 10, 2015 at 4:19
  • Thanks, That solved the Second Problem! Much Apprectiated! Commented Sep 10, 2015 at 4:34

1 Answer 1

1

Looks like you are trying to set general_report on an instance of a GenRepEnt class but that method doesn't exist. This is likely because the association was not set up in the GenRepEnt model.

In the model gen_rep_ent.rb add the following association:

belongs_to :general_report

After you declare this association Rails will define a few methods on each instance of GenRepEnt including:

general_report
general_report=

You will also need to generate a migration that will add a new column, general_report_id, to the gen_rep_ents table.

In your terminal run rails g migration AddGeneralReportRefToGenRepEnts general_report:references

This should generate a migration that looks something like this:

class AddGeneralReportRefToGenRepEnts < ActiveRecord::Migration
  def change
    add_reference :gen_rep_ents, :general_report, index: true, foreign_key: true
  end
end

Next, run the migration using rake db:migrate and restart your app.

Read more about the belongs_to association here.

Sign up to request clarification or add additional context in comments.

2 Comments

so after I added belongs_to :general_report to the GenRepEnt model i get the exact same error after doing a server restart and loged out and logged back into the app..?
that worked, i think however now i get the following (ill also update question) ActiveRecord::AssociationTypeMismatch in GeneralReports::GenRepEntsController#create and still refrenced the same old problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.