2016-08-19 5 views
0

Ein wenig Hintergrund, nahm ich ein Projekt über die jemand anderes gestartet und hat für 8ish Monate nicht gearbeitet. Bei dem Projekt handelt es sich um eine CRM-Anwendung, die mit Rails 4 erstellt wurde. Ich habe ein paar Probleme damit, dort aufzutauchen, wo sie aufgehört haben, und suche nach Hilfe von erfahrenen Rails-Entwicklern. Der Fehler, den ich erhalte, ist, wenn ich versuche, einen neuen Job von einer Job-Tracker-Seite hinzuzufügen. Der Fehler, den ich erhalte ist:Rails 4 NoMethodError in Jobs # new

ActionView::Template::Error (undefined method `opportunity' for #<Job:0x62d0240>): 
    1: <% @job[:opportunity_id] = params[:opportunity_id] %> 
    2: <% title "New #{@job.opportunity.name} Job"%> 
    3: 
    4: <% 
    5: @job[:name] = @job.opportunity.name 
    app/views/jobs/new.html.erb:2:in `_app_views_jobs_new_html_erb___882142983_51776136' 

und der Fehler tritt auf Zeile 2 der oben genannten. Ich werde den relevanten Code posten, lassen Sie mich wissen, wenn ich noch etwas hinzufügen muss. Danke im Voraus!

Jobs neue Ansicht (wo Fehler auftreten)

<% @job[:opportunity_id] = params[:opportunity_id] %> 
<% title "New #{@job.opportunity.name} Job"%> 

<% 
@job[:name] = @job.opportunity.name 
@pm = @job.opportunity.pm_id 


%> 

<br><br> 
<%= render 'form' %> 

Gelegenheit Controller-

class OpportunitiesController < ApplicationController 
    before_action :set_opportunity, only: [:show, :edit, :update, :destroy] 
    load_and_authorize_resource 
    # GET /opportunities 
    # GET /opportunities.json 
    def index 
    @opportunities = Opportunity.all 
    end 

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

    # GET /opportunities/new 
    def new 
    @opportunity = Opportunity.new 
    end 

    # GET /opportunities/1/edit 
    def edit 
    end 

    # POST /opportunities 
    # POST /opportunities.json 
    def create 
    @opportunity = Opportunity.new(opportunity_params) 

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

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

    # DELETE /opportunities/1 
    # DELETE /opportunities/1.json 
    def destroy 
    @opportunity.destroy 
    respond_to do |format| 
     format.html { redirect_to opportunities_url, notice: 'Opportunity was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_opportunity 
     @opportunity = Opportunity.find(params[:id]) 
     @film_specs = @opportunity.film_specs.all 
     @digital_specs = @opportunity.digital_specs.all 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def opportunity_params 
     params.require(:opportunity).permit(:employee_id, :emp2_id, :emp3_id, :name, :prop_date, :opp_status_id, :delay, :won, :lost, :con_signed, :quote_won_id, :total_cost, :exp_close, :pri_comp_id, :notes, :location, :pm_id) 
    end 
end 

Job Controller

class JobsController < ApplicationController 
    before_action :set_job, only: [:show, :edit, :update, :destroy] 
    skip_load_and_authorize_resource 
    # GET /jobs 
    # GET /jobs.json 
    def index 
    @jobs = Job.all 
    end 

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

    # GET /jobs/new 
    def new 
    @job = Job.new do |j| 
     if params[:opportunity_id].present? 
     j.opportunity_id = params[:opportunity_id] 
     end 
    end 
    end 

    # GET /jobs/1/edit 
    def edit 
    end 

    # POST /jobs 
    # POST /jobs.json 
    def create 
    @job = Job.new(job_params) 

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

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

    # DELETE /jobs/1 
    # DELETE /jobs/1.json 
    def destroy 
    @job.destroy 
    respond_to do |format| 
     format.html { redirect_to jobs_url, notice: 'Job was successfully deleted.' } 
     format.json { head :no_content } 
    end 
    end 

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def job_params 
     params.require(:job).permit(:opportunity_id, :number, :name, :flight_date, :flight_sub, :camera, :roll, :map_type, :plan_only, :lab_only, :est_hrs_model, :due_date, :edge_job_id, :custom_trans, :comp_inhouse, :delivered_date, :done, :control_in, :control_status, :at_date, :control_results, :control_check, :scan_staff, :scan_date, :scan_check, :comp_staff, :comp_date, :comp_check, :comp_sub, :comp_sub_due_date, :comp_sub_rec, :img_staff, :img_date, :img_check, :edit_staff, :edit_date, :edit_check, :notes, :file1, :file2, :file3, :file4, :file5, :add_files) 
    end 
end 

Gelegenheit Modell

class Opportunity < ActiveRecord::Base 
    belongs_to :employee 
    has_one :user 
    has_many :film_specs 
    has_many :digital_specs 
    has_many :film_quotes 
    has_many :cost_proposals 
    has_many :jobs 

    validates_presence_of :opp_status_id 
end 

Job Modell

class Job < ActiveRecord::Base 
    mount_uploader :file1, AttachmentUploader 
    belongs_to :cost_proposal 
    has_many :opportunities 
end 

Job Schema

create_table "jobs", force: true do |t| 
    t.integer "cost_proposal_id" 
    t.string "number" 
    t.string "name" 
    t.date  "flight_date" 
    t.string "flight_sub" 
    t.string "camera" 
    t.string "roll" 
    t.string "map_type" 
    t.integer "plan_only" 
    t.integer "lab_only" 
    t.integer "est_hrs_model" 
    t.date  "due_date" 
    t.integer "edge_job_id" 
    t.integer "custom_trans" 
    t.integer "comp_inhouse" 
    t.date  "delivered_date" 
    t.integer "done" 
    t.date  "control_in" 
    t.string "control_status" 
    t.date  "at_date" 
    t.string "control_results" 
    t.integer "control_check" 
    t.string "scan_staff" 
    t.date  "scan_date" 
    t.integer "scan_check" 
    t.string "comp_staff" 
    t.date  "comp_date" 
    t.integer "comp_check" 
    t.string "comp_sub" 
    t.date  "comp_sub_due_date" 
    t.integer "comp_sub_rec" 
    t.string "img_staff" 
    t.date  "img_date" 
    t.integer "img_check" 
    t.string "edit_staff" 
    t.date  "edit_date" 
    t.integer "edit_check" 
    t.text  "notes" 
    t.string "file1" 
    t.string "file2" 
    t.string "file3" 
    t.string "file4" 
    t.string "file5" 
    t.string "add_files" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "flown" 
    t.integer "cust_trans" 
    t.integer "delivered" 
    t.string "at_staff" 
    t.integer "at_check" 
    t.integer "opportunity_id" 
    end 

Gelegenheit Schema

create_table "opportunities", force: true do |t| 
    t.integer "employee_id" 
    t.integer "emp2_id" 
    t.integer "emp3_id" 
    t.string "name" 
    t.datetime "prop_date" 
    t.integer "opp_status_id" 
    t.string "delay" 
    t.date  "con_signed" 
    t.integer "quote_won_id" 
    t.float "total_cost" 
    t.date  "exp_close" 
    t.integer "pri_comp_id" 
    t.text  "notes" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "lost" 
    t.string "won" 
    t.string "location" 
    t.integer "pm_id" 
    t.integer "job_id" 
    end 

Antwort

1

Sie haben keine opportunity Methode, weil es sich um eine has_many Beziehung ist, so dass Sie opportunities und ist ein Array haben. Aber Sie haben die opportunitity_id, so dass Sie Ihr Opportunity-Objekt finden können.

opportunity = Opportunity.find(params[:opportunity_id]) 
+0

Nein, Sie können es überall verwenden, wo Sie es brauchen. Die erste Zeile, wo Sie brauchen Ich denke, das ist: '<% title "New #{@job.opportunity.name} Job" %>' und werden '<% title "New # {} opportunity.name Job" %> ' – Ursus

+0

die Zeile zu ändern auf <% title "New #{@opportunity.name} Job" %> gibt mir eine Fehlermeldung "nicht definierte Methode 'Name‘ für nil: NilClass". Vielen Dank im Voraus – kmaune

+0

sicher, weil Sie haben es irgendwo vor der Möglichkeit Objekt zuweisen '@opportunity = Opportunity.find (params [: opportunity_id])' – Ursus