Ruby on rails Rails-通过关系表在视图上显示类别名称
我有两个表,我把它们与博客和博文类别的关系放在一起。以下是模式:Ruby on rails Rails-通过关系表在视图上显示类别名称,ruby-on-rails,Ruby On Rails,我有两个表,我把它们与博客和博文类别的关系放在一起。以下是模式: create_table "blogs", force: :cascade do |t| t.string "title" t.text "body" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "slug" t.integer "status", defau
create_table "blogs", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.integer "status", default: 0
t.bigint "post_category_id"
t.index ["post_category_id"], name: "index_blogs_on_post_category_id"
t.index ["slug"], name: "index_blogs_on_slug", unique: true
end
create_table "post_categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
现在我要做的是在我的blog index.html.erb文件中显示类别名称而不是类别id。我做了以下操作,但没有工作:
<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<% PostCategory.all.each do |c| %>
<% if c.id == blog.post_category %>
<td><%= c.name %>
<% end %>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
注意:我还尝试通过
直接访问它,但仍然无法工作
知道如何显示类别名称而不是关联的类别id吗?型号更改
class PostCategory < ApplicationRecord
has_many :blogs
end
class Blog < ApplicationRecord
belongs_to :post_category
end
class PostCategory < ApplicationRecord
has_many :blogs
end
class Blog < ApplicationRecord
belongs_to :post_category
end
类后分类
视图更改
<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.try(:name) %></td>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?'} %></td>
</tr>
<% end %>
<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.name rescue 'No post category present for this blog' %></td>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?'} %></td>
</tr>
<% end %>
根据您提供的模式,请注意PostCategory对象有许多Blog对象 模式改变
class PostCategory < ApplicationRecord
has_many :blogs
end
class Blog < ApplicationRecord
belongs_to :post_category
end
class PostCategory < ApplicationRecord
has_many :blogs
end
class Blog < ApplicationRecord
belongs_to :post_category
end
类后分类
视图更改
<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.try(:name) %></td>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?'} %></td>
</tr>
<% end %>
<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.title %></td>
<td><%= blog.body %></td>
<td><%= blog.post_category.name rescue 'No post category present for this blog' %></td>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?'} %></td>
</tr>
<% end %>
PG::UndefinedColumn:错误:column post\u categories.blog\u id不存在第1行:。。。“发布类别”。*来自“发布类别”,其中“post_cate…首先重置数据库并开始创建为什么调用post_categories.blog_iddown<代码>博客所属:博士后类别作为博客
表有foregin键博士后类别id
属于:博士后类别作为博客
将其放在我的博客模型上,但我得到了以下结果:编译错误\GET/blogs.json def index@blogs=blog.all end def post类别
如果我使用此
它给了我这样的信息:#
RAY已经尝试过了,但是得到了nil的未定义方法“name”:nilclass更新的答案,这是您的数据问题,因为关联和模式是正确的,但您没有关联的数据。使用try
方法可以处理issue@MarcSolva对于blog.post\u categories\u id
,表post\u categories
中必须有id,它可以工作,但让“try”方法处理工作是否安全?这是一种好的做法吗?还可以进一步解释关联问题吗?RAY-我是否需要为post_categories表添加迁移id来解决此问题?