无法找到没有ID的[Model] - rails4

时间:2015-05-31 20:00:55

标签: ruby-on-rails ruby-on-rails-4

我是新手,并且遇到了一个难以克服的问题。

目标:

  • 我有模型广告&表格
  • 广告has_many形式| form belongs_to advert
  • 我正在尝试显示广告
  • 下的表单列表
  • 我已设法显示广告下的表单数量并将其设为链接
  • 在我的图片示例中,我有一个(工作)广告"软件工程师"其中有2个表单(应用程序)作为链接提供
  • 我希望用户点击链接(图中)并转到显示该广告下所有表单的页面

enter image description here

  • 目前,当我点击链接(在应用程序下)时,我将被定向到路径forms_path,该路径根据表单控制器指示显示所有表单
  

可以告诉我如何在表单中编写索引方法   控制器不显示所有创建的表单,而只显示a下的表单   特定广告

advert_controller.rb

class AdvertsController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_advert, only: [:show, :edit, :update, :destroy]

  def index
    @userr = Userr.find(params[:userr_id])
    @adverts = @userr.adverts.order("created_at DESC")
    respond_with(@adverts)
  end
end

views / adverts / index.html

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>applications</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @adverts.each do |advert| %>
      <tr>
        <td><%= link_to advert.title, userr_advert_path(advert.userr, advert) %></td>
        <td><%= link_to advert.forms.count, forms_path %></td>
      </tr>
    <% end %>
  </tbody>
</table>
<br>

<%= link_to 'add new advert', new_userr_advert_path(current_userr) %>

form_controller.rb

class FormsController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_form, only: [:show, :edit, :update, :destroy]

  def index
    @forms = Form.all
    respond_with(@forms)
  end
end

的routes.rb

Rails.application.routes.draw do
  resources :forms
  devise_for :userrs
  resources :userrs do
    resources :adverts
  end
end

在我的rails控制台中 - 我设法显示广告下的所有表格:

2.1.2 :308 >   advert = Advert.first
  Advert Load (4.4ms)  SELECT  "adverts".* FROM "adverts"   ORDER BY "adverts"."id" ASC LIMIT 1
 => #<Advert id: 51, title: "software engineer", content: "Lorem ipsum dolor sit amet ac dapibus", category_jobtype_id: 67, category_positiontype_id: 81, salarystart: 1200, salaryend: 2000, category_country_id: 45, city: "accra", town: "tesano estates", postcode: "1206", category_editorialapproval_id: 34, category_applicationrequest_id: 34, created_at: "2015-05-21 21:05:09", updated_at: "2015-05-21 21:05:09", userr_id: 16, category_advert_id: 58> 
2.1.2 :309 > 
2.1.2 :310 >   
2.1.2 :311 >   advert.forms
  Form Load (0.2ms)  SELECT "forms".* FROM "forms"  WHERE "forms"."advert_id" = ?  [["advert_id", 51]]
 => #<ActiveRecord::Associations::CollectionProxy [#<Form id: 4, firstname: "akunorbea", lastname: "artloe", number: 2089587999, email: "akunorbea@gmail.com", currentJob: "developer", currentEmployer: "global reach", category_country_id: 2, advert_id: 51, userj_id: 3, workhere: false, created_at: "2015-05-29 09:09:33", updated_at: "2015-05-29 09:09:33">, #<Form id: 6, firstname: "curtis", lastname: "lewis", number: 208958, email: "curtis@gmail.com", currentJob: "security", currentEmployer: "megan fox", category_country_id: 2, advert_id: 51, userj_id: 4, workhere: false, created_at: "2015-05-29 17:14:41", updated_at: "2015-05-29 17:14:41">]> 
2.1.2 :312 >

所以在我的form_controller.rb索引操作中我输入了以下内容但是我收到了错误

class FormsController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_form, only: [:show, :edit, :update, :destroy]

  def index
    @advert = Advert.find(params[:id])
    @forms = @advert.forms
    respond_with(@forms)
  end
end

视图/形式/ index.html.erb

<h1>Listing forms</h1>

<table>
  <thead>
    <tr>
      <th>Firstname</th>
      <th>Lastname</th>
      <th>Number</th>
      <th>Email</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @forms.each do |form| %>
      <tr>
        <td><%= form.firstname %></td>
        <td><%= form.lastname %></td>
        <td><%= form.number %></td>
        <td><%= form.email %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

1 个答案:

答案 0 :(得分:2)

在routes.rb

resources :userrs do
  resources :adverts do
    resources :forms, only: [:index]
  end
end

in views / adverts / index.html.erb

<td><%= link_to advert.forms.count, userr_advert_forms_path(@userr, advert) %></td>
form_controller.rb中的

def index
  @advert = Advert.find(params[:advert_id])
  @forms = advert.forms
end