Rails 4作业中的NoMethodError#new

时间:2016-08-19 16:40:52

标签: ruby-on-rails ruby

有一点背景,我接手了一个别人开始并且已经工作了8个月的项目。该项目是使用Rails 4构建的CRM应用程序。我在接下来的地方遇到一些麻烦,并且正在寻找经验丰富的Rails开发人员的帮助。我收到的错误是当我尝试从作业跟踪页面添加新作业时。我收到的错误是:

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'

并且错误发生在上面的第2行。我会发布相关代码,如果我需要添加其他内容,请告诉我。提前谢谢!

作业新视图(发生错误的地方)

<% @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' %>

机会控制器

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

职位控制器

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

机会模式

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

工作模式

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

作业架构

  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

机会架构

  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

1 个答案:

答案 0 :(得分:0)

您没有opportunity方法,因为它是has_many关系,因此您拥有opportunities并且是一个数组。但是你有opportunitity_id所以你可以找到你的机会对象。

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