【发布时间】:2018-02-15 08:54:24
【问题描述】:
我有一个Roast 模型,其中有很多Countries,其中有很多Regions。
我很难让国家和地区显示在我的烤肉展示视图中。我可以看到我的 Roast 模型中有 country_id 和 region_id 的值,所以我知道这些值已正确插入。
以目前的形式,它实际上并没有抛出任何错误,所以我有点不知道接下来要尝试什么?
从日志看来,它试图查找国家/地区值,但我看不到区域:
Processing by RoastsController#show as HTML
Parameters: {"id"=>"belong_to"}
Roast Load (2.3ms) SELECT "roasts".* FROM "roasts" WHERE "roasts"."slug" = $1 LIMIT $2 [["slug", "belong_to"], ["LIMIT", 1]]
CACHE Roast Load (0.0ms) SELECT "roasts".* FROM "roasts" WHERE "roasts"."slug" = $1 LIMIT $2 [["slug", "belong_to"], ["LIMIT", 1]]
Roaster Load (0.4ms) SELECT "roasters".* FROM "roasters" WHERE "roasters"."roaster_id" = $1 LIMIT $2 [["roaster_id", 3], ["LIMIT", 1]]
Rendering roasts/show.html.erb within layouts/application
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."user_id" = $1 ORDER BY "users"."user_id" ASC LIMIT $2 [["user_id", 15], ["LIMIT", 1]]
Rendered users/shared/_links.html.erb (1.5ms)
Rendered partials/_mainnav.html.erb (15.6ms)
Country Load (0.4ms) SELECT "countries".* FROM "countries" WHERE "countries"."roast_id" = $1 [["roast_id", 76]]
Note Load (0.3ms) SELECT "notes".* FROM "notes" INNER JOIN "tastings" ON "notes"."id" = "tastings"."note_id" WHERE "tastings"."roast_id" = $1 [["roast_id", 76]]
(0.6ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 [["commentable_id", 76], ["commentable_type", "Roast"]]
Rendered comments/_form.html.erb (3.9ms)
(0.7ms) SELECT COUNT(*) FROM "roasts" WHERE "roasts"."roaster_id" = 3
Roast Load (0.7ms) SELECT "roasts".* FROM "roasts" WHERE "roasts"."roaster_id" = 3
Rendered partials/_footer.html.erb (1.4ms)
Rendered roasts/show.html.erb within layouts/application (42.1ms)
Completed 200 OK in 274ms (Views: 260.5ms | ActiveRecord: 6.0ms)
模型
class Roast < ApplicationRecord
has_many :tastings
has_many :countries
has_many :notes, through: :tastings
has_many :comments, as: :commentable
accepts_nested_attributes_for :countries
has_many :regions
belongs_to :roaster
class Country < ApplicationRecord
has_many :regions, inverse_of: :country
accepts_nested_attributes_for :regions
belongs_to :roast
class Region < ApplicationRecord
belongs_to :country, inverse_of: :regions
roasts/show.html.erb
<tbody>
<% @roast.countries.each do |countries| %>
<tr>
<th>
<%= roast.countries.country_name %>
</th>
<% @roast.countries.region.each do |region| %>
<th>
<%= region.region_name %>
</th>
</tr>
<% end %>
<% end %>
</tbody>
roast_controller.rb
def show
@roast = Roast.friendly.find(params[:id])
@commentable = @roast
@comments = @commentable.comments
@comment = Comment.new
@sameroaster = Roast.where(roaster: @roast.roaster)
@samecountry = Roast.where(country: @roast.country)
@roastcount = Roast.where(roaster: @roast.roaster)
@countries = @roast.countries
@regions = @roast.regions
end
架构
create_table "countries", force: :cascade do |t|
t.string "country_name"
t.integer "region_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "roast_id"
t.string "slug"
end
create_table "regions", force: :cascade do |t|
t.string "region_name"
t.integer "country_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "roasts", force: :cascade do |t|
t.string "roaster"
t.string "name"
t.string "bestfor"
t.string "beans"
t.string "roast"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "tastingnotes"
t.string "slug"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "roaster_id"
t.integer "country_id"
t.boolean "decaf"
t.boolean "coldbrew"
t.integer "region_id"
t.index ["country"], name: "idx_roasts_country"
t.index ["country_id"], name: "idx_roasts_country_id"
t.index ["roaster_id"], name: "idx_roasts_roaster_id"
【问题讨论】:
-
将控制器代码粘贴到您要查询区域的位置?以及您希望如何在页面上显示它?
-
@NarasimhaReddy 刚刚添加到帖子中。
标签: ruby-on-rails nested-forms