nil的未定义方法`jobs':NilClass

时间:2014-07-15 10:56:45

标签: ruby-on-rails ruby

我是铁轨上的红宝石的初学者并且得到了一个我似乎不理解的错误。

在Tradesman的个人资料页面下,我试图调用交易中的所有交易和所有列出的工作。贸易有很多就业机会工作has_many交易。

TrademanprofilesController:

class TrademanprofilesController < ApplicationController

  def show
    @user = current_user   
  end

  def jobleads   
  end

  def purchased   
  end

  def memberBenifits   
  end

  def account   
  end

  def editjoblead
    @trades = Trade.order(:name)
    @jobs = Job.all

    if params[:search].present?
      @applications = Application.near(params[:search], 100, order: 'distance')
      @hash = Gmaps4rails.build_markers(@applications) do |application, marker|
        marker.lat application.latitude
        marker.lng application.longitude
        marker.infowindow application.location
        marker.infowindow application.trade.name
      end
    else
      @applications = Application.all
    end
    @applications_trades = @applications.group_by { |t| t.trade_id }   end end

在其他地方调用的部分视图页面。 _trademanprofile_skills.html.erb:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @trades.each do |trade| %>
      <tr>
        <td>
            <%= trade.name %></br>
            <ul><% @trade.jobs.each do |job| %></ul>
                <li><%= job.name %></li>
            <% end %>
        </td>
        <td><%= link_to 'Show', trade %></td>
      </tr>
    <% end %>
  </tbody>
</table>

型号:Trade.rb

class Trade < ActiveRecord::Base
    attr_accessible :name, :job_ids

    has_many :applications
    has_many :jobs, through: :applications
end

model:Job.rb

class Job < ActiveRecord::Base
    attr_accessible :name, :trade_ids

    has_many :applications
    has_many :trades, through: :applications
end

我得到的错误:

NoMethodError in Trademanprofiles#editjoblead
undefined method `jobs' for nil:NilClass
and highlights this line: 
<ul><% @trade.jobs.each do |job| %></ul>
<li><%= job.name %></li>
<% end %>

我尝试了以下但没有工作

<% @trades.each do |trade| %>
<%= trade.name %>
<%= trade.jobs %>
<% end %>
i got this displayed:
#<Job::ActiveRecord_Associations_CollectionProxy:0x00000106b0f1d8>

我也尝试了以下但没有工作

<% @trades.each do |trade| %>
<%= trade.name %>
<%= trade.job_ids %>
<% end %>
but it only returned the id numbers of the jobs under the trade Aerial / Network 
Aerial / Network Specialist
[978, 979, 980, 1039, 1040, 1041]

我的路线档案:

Rails.application.routes.draw do

  resources :images

  devise_for :users
  resources :jobstartdates

  resources :budgets

  resources :applications do
    collection do
      get :search
      get :search_result
      get :business_details
    end
  end

  get 'home/index'

  resources :trades

  resources :jobs

  root to: 'home#index', as: 'home'

  resource :trademanprofiles do
    get :show
    get :jobleads
    get :purchased
    get :memberBenifits
    get :account
    get :editjoblead
  end

  resources :employeenumbers

  resources :businesstypes

  resources :trademanroles

  resources :titles

  resources :adverts

  resources :distances

我想要商品名称&amp;要显示的作业名称(而不是ID)。非常感谢,如果这是一个简单的问题,我真的很抱歉。

2 个答案:

答案 0 :(得分:2)

如果你看一下你的方法

def editjoblead
  @trades = Trade.order(:name) #this will return a collection of trades to you
  @jobs = Job.all
  #other logic
end

并且在您看来,你有这个

<ul>
  <% @trade.jobs.each do |job| %></ul>
  <li><%= job.name %></li>
  <% end %>

首先,您需要更正已关闭的ul html,然后使用li,然后再使用来修正收藏集合(@trade是一个集合和工作将给你一个集合)

你需要这样做:

<% @trades.each do |trade| %>
  // this will give a single trade present in @trades
  <tr>
    <td>
        <%= trade.name %></br>
        <ul>
          <% trade.jobs.each do |job| %>
            // this will give a job associated with your trade, notice it's trade and not @trade
            <li><%= job.name %></li>
          <% end %>
        </ul>
    </td>
    <td><%= link_to 'Show', trade %></td>
  </tr>
<% end %>

答案 1 :(得分:0)

你的routes.rb文件怎么样?我的错误可能就在你的路线文件

resources :trades do
   resources :applications
   resources :jobs 
end