【问题标题】:Nested Values not Displaying in Show View Rails 5.1在 Show View Rails 5.1 中不显示嵌套值
【发布时间】: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


【解决方案1】:

根据问题

如果你愿意

Roast 有很多国家

国家有很多地区

进行以下更改

  create_table "roasts", force: :cascade do |t|
    t.integer "country_id" #remove country_id column from roasts
    t.integer "region_id"  #remove region_id column from roasts
    t.index ["country_id"], name: "idx_roasts_country_id" #remove
  end

  create_table "countries", force: :cascade do |t|
    t.integer "region_id" #remove region_id column from countries
  end

你的模型结构应该是这样的

class Roast < ApplicationRecord
  has_many :tastings
  has_many :countries
  has_many :notes, through: :tastings
  has_many :comments, as: :commentable
  accepts_nested_attributes_for :countries
  #remove has_many regions for roast
  # because as per your question country has many regions not roast
  has_many :regions #remove this
  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
end

控制器动作和视图应该类似于

def show
      @roast = Roast.includes(countries: :regions)
                    .friendly
                    .find(params[:id])
      @commentable = @roast
      @comments = @commentable.comments
      @comment = Comment.new
      @sameroaster = Roast.where(roaster: @roast.roaster)
      @roastcount = Roast.where(roaster: @roast.roaster).count

    end

观看次数

<table>
   <thead>
     <tr>
       <th>#</th>
       <th>Country</th>
       <th>Regions</th>
     </tr>
   </thead>
    <tbody>
    <% @roast.countries.each_with_index do |country, index| %>
      <tr>
        <td><%= index + 1 %></td>
        <td><%= country.name %></td> // change this to country_name if your field is different in contries schema.
        <td><%= country.regions.map(&:name).join(', ') %></td>// change this to region_name if your field is different in regions schema.
      </tr>
    <% end %>
  </tbody>
 </table>

我希望这会有所帮助。

【讨论】:

  • 谢谢@Narasimha。不幸的是,这并不能解决问题。没有错误消息,但我可以在日志中看到它仍然没有找到 country_id Country Load (0.7ms) SELECT "countries".* FROM "countries" WHERE "countries"."roast_id" = 74
  • 是的,只是和预期的一样。没有错。接下来它应该加载区域。如果你不是我的,只需用最近的日志更新问题中的日志
  • 对于 `roast_id 74,我可以在数据库中看到它的 country_id=99 和 region_id=81。所以绝对重视那里。链接或关联显然有问题,但无法解决。
  • 如果没有您的表模式,就很难回答。如果您添加我可以查看它
  • 添加了架构
猜你喜欢
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多