ruby on rails - Couldn't find [Model] without an ID - rails4 -
i new rails , have problem have been finding challenging overcome.
goal:
- i have models advert & form
- advert has_many forms | form belongs_to advert
- i trying display list of forms under advert
- i have managed display number of forms under advert , made link
- in image example, have (job) advert "software engineer" has 2 forms (applications) presented link
- i want user click on link (the figure) , directed page displays forms under advert
- currently when click link (under applications) directed path
forms_path
displays form per form controller instructs
could 1 advise me how can code index method in forms controller display not created forms forms under 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 |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 resources :forms devise_for :userrs resources :userrs resources :adverts end end
in rails console - managed display forms under advert:
2.1.2 :308 > advert = advert.first advert load (4.4ms) select "adverts".* "adverts" order "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".* "forms" "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 >
so in form_controller.rb index action typed below got 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 |form| %> <tr> <td><%= form.firstname %></td> <td><%= form.lastname %></td> <td><%= form.number %></td> <td><%= form.email %></td> </tr> <% end %> </tbody> </table> <br>
in routes.rb
resources :userrs resources :adverts 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
Comments
Post a Comment