我是新手,并且遇到了一个难以克服的问题。
目标:
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>
答案 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