ARTLoe
ARTLoe

Reputation: 1799

Couldn't find [Model] without an ID - rails4

i am new to rails and have a problem that have been finding challenging to overcome.

goal:

enter image description here

could one advise me how i can code the index method in the forms controller to display not all created forms but only the forms under a specific advert

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

in my rails console - i managed to display all the forms under an advert:

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: "[email protected]", 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: "[email protected]", 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 >

so in my form_controller.rb index action i typed the below but i got an error

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

views/forms/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>

Upvotes: 2

Views: 128

Answers (1)

margo
margo

Reputation: 2927

in 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>

in forms_controller.rb

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

Upvotes: 2

Related Questions