首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 4 NoMethodError in Jobs#new

Rails 4 NoMethodError in Jobs#new
EN

Stack Overflow用户
提问于 2016-08-19 16:40:51
回答 1查看 137关注 0票数 0

一个小小的背景,我接手了一个其他人开始的项目,已经有8个月没有工作了。这个项目是一个使用Rails 4构建的CRM应用程序,我遇到了一些小麻烦,我很难找到他们的故障,并从经验丰富的Rails开发人员那里寻求帮助。当我试图从作业跟踪器页面中添加一个新职务时,我收到的错误是。我收到的错误是:

代码语言:javascript
复制
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行。我会张贴相关的代码,让我知道,如果我需要添加任何其他。提前感谢!

作业新视图(出现错误的地方)

代码语言:javascript
复制
<% @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' %>

机会控制器

代码语言:javascript
复制
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

作业控制器

代码语言:javascript
复制
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

机会模型

代码语言:javascript
复制
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

作业模型

代码语言:javascript
复制
class Job < ActiveRecord::Base
  mount_uploader :file1, AttachmentUploader
  belongs_to :cost_proposal
  has_many :opportunities
end

作业模式

代码语言:javascript
复制
  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

机会模式

代码语言:javascript
复制
  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
EN

回答 1

Stack Overflow用户

发布于 2016-08-19 16:44:10

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

代码语言:javascript
复制
opportunity = Opportunity.find(params[:opportunity_id])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39044015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档