Rails:在 rails 服务器中加载部分对象但未呈现
Rails: Partial object loading in rails server but not rendering
我正在尝试在 show.html.erb
文件中为一个名为 'flag' 的对象渲染一个名为 'team' 的对象的部分。
但是,团队的已分配和已提升标志的部分不会呈现。
我 运行 遇到了问题,因为这是一个复杂的关联。
一面旗帜可以由一个团队升起,但可以分配给另一个团队。这是通过使用 assignedto_id
和 raisedby_id
来引用的。这些 ID 是创建标志对象时的 team_id
。两者都是 belongs_to 协会。
然后,一个团队可以有许多旗帜升起,也有许多旗帜分配给他们。我希望显示分配给团队并举起的旗帜。
如何渲染它? teams_controller.rb
文件中是否需要添加任何内容?
请看下面的代码。
Flag.rb
class Flag < ApplicationRecord
belongs_to :raisedby, :class_name => 'Team'
belongs_to :assignedto, :class_name => 'Team'
belongs_to :user
enum priority: [:high, :medium, :low]
enum status: [:raised, :open, :closed, :cancelled]
validate :check_assigned_and_raised
def check_assigned_and_raised
errors.add(:assignedto_id, " error has occurred. The assigned team can not be the same as team who is raising the flag.") if assignedto_id == raisedby_id
end
end
Team.rb
class Team < ApplicationRecord
has_many :users
has_many :assignedFlags, -> {where 'assignedto_id = "team_id"'}, :class_name => 'Flag', :foreign_key => 'assignedto_id'
has_many :raisedFlags, -> {where 'raisedby_id = "team_id"'}, :class_name => 'Flag', :foreign_key => 'raisedby_id'
end
teams/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<br>
<%= @team.name %>
</p>
<p>
<br>
<strong>Description:</strong>
<br>
<%= @team.description %>
</p>
<br>
<strong>Users:</strong>
<p>
<% @team.users.each do |user| %>
<%= render partial: 'users/user', locals: { user: user } %>
<% end %>
</p>
<br>
<strong>Assigned Flags:</strong>
<p>
<% @team.assignedFlags.each do |flag| %>
<%= render partial: 'flags/flag', locals: { flag: flag } %>
<% end %>
</p>
<br>
<strong>Raised Flags:</strong>
<p>
<% @team.raisedFlags.each do |flag| %>
<%= render partial: 'flags/flag', locals: { flag: flag } %>
<% end %>
</p>
<br>
<%= link_to 'Edit', edit_team_path(@team) %> |
<%= link_to 'Back', teams_path %>
_flags.html.erb
<div>
<p>
<strong>Title:</strong>
<%= @flag.title %>
</p>
<p>
<strong>Description:</strong>
<%= @flag.description %>
</p>
<p>
<strong>Priority:</strong>
<%= @flag.priority %>
</p>
<p>
<strong>Status:</strong>
<%= @flag.status %>
</p>
<p>
<strong>Raisedby:</strong>
<%= @flag.raisedby.name %>
</p>
<p>
<strong>Assignedto:</strong>
<%= @flag.assignedto.name %>
</p>
<p>
<strong>Created By:</strong>
<%= @flag.user.name if @flag.user %>
<%= @flag.user.surname if @flag.user %>
</p>
<%= link_to 'Edit', edit_flag_path(@flag) %> |
<%= link_to 'Back', flags_path %>
</div>
Rails 服务器结果
Team Load (0.4ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering teams/show.html.erb within layouts/application
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."team_id" = ? [["team_id", 1]]
Rendered users/_user.html.erb (0.7ms)
Rendered users/_user.html.erb (0.1ms)
Flag Load (0.5ms) SELECT "flags".* FROM "flags" WHERE "flags"."assignedto_id" = ? AND (assignedto_id = "team_id") [["assignedto_id", 1]]
Flag Load (0.3ms) SELECT "flags".* FROM "flags" WHERE "flags"."raisedby_id" = ? AND (raisedby_id = "team_id") [["raisedby_id", 1]]
Rendered teams/show.html.erb within layouts/application (12.3ms)
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
Completed 200 OK in 138ms (Views: 129.8ms | ActiveRecord: 2.0ms)
它没有显示,因为你需要使用 flag
而不是 @flag,还优化了你的代码,请检查
<p>
<strong>Title:</strong>
<%= flag.title %>
</p>
<p>
<strong>Description:</strong>
<%= flag.description %>
</p>
<p>
<strong>Priority:</strong>
<%= flag.priority %>
</p>
<p>
<strong>Status:</strong>
<%= flag.status %>
</p>
<p>
<strong>Raisedby:</strong>
<%= flag.raisedby.name %>
</p>
<p>
<strong>Assignedto:</strong>
<%= flag.assignedto.name %>
</p>
<p>
<strong>Created By:</strong>
<%= flag.user&.name %>
<%= flag.user&.surname %>
</p>
<%= link_to 'Edit', edit_flag_path(flag) %> |
<%= link_to 'Back', flags_path %>
</div>
我正在尝试在 show.html.erb
文件中为一个名为 'flag' 的对象渲染一个名为 'team' 的对象的部分。
但是,团队的已分配和已提升标志的部分不会呈现。
我 运行 遇到了问题,因为这是一个复杂的关联。
一面旗帜可以由一个团队升起,但可以分配给另一个团队。这是通过使用 assignedto_id
和 raisedby_id
来引用的。这些 ID 是创建标志对象时的 team_id
。两者都是 belongs_to 协会。
然后,一个团队可以有许多旗帜升起,也有许多旗帜分配给他们。我希望显示分配给团队并举起的旗帜。
如何渲染它? teams_controller.rb
文件中是否需要添加任何内容?
请看下面的代码。
Flag.rb
class Flag < ApplicationRecord
belongs_to :raisedby, :class_name => 'Team'
belongs_to :assignedto, :class_name => 'Team'
belongs_to :user
enum priority: [:high, :medium, :low]
enum status: [:raised, :open, :closed, :cancelled]
validate :check_assigned_and_raised
def check_assigned_and_raised
errors.add(:assignedto_id, " error has occurred. The assigned team can not be the same as team who is raising the flag.") if assignedto_id == raisedby_id
end
end
Team.rb
class Team < ApplicationRecord
has_many :users
has_many :assignedFlags, -> {where 'assignedto_id = "team_id"'}, :class_name => 'Flag', :foreign_key => 'assignedto_id'
has_many :raisedFlags, -> {where 'raisedby_id = "team_id"'}, :class_name => 'Flag', :foreign_key => 'raisedby_id'
end
teams/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<br>
<%= @team.name %>
</p>
<p>
<br>
<strong>Description:</strong>
<br>
<%= @team.description %>
</p>
<br>
<strong>Users:</strong>
<p>
<% @team.users.each do |user| %>
<%= render partial: 'users/user', locals: { user: user } %>
<% end %>
</p>
<br>
<strong>Assigned Flags:</strong>
<p>
<% @team.assignedFlags.each do |flag| %>
<%= render partial: 'flags/flag', locals: { flag: flag } %>
<% end %>
</p>
<br>
<strong>Raised Flags:</strong>
<p>
<% @team.raisedFlags.each do |flag| %>
<%= render partial: 'flags/flag', locals: { flag: flag } %>
<% end %>
</p>
<br>
<%= link_to 'Edit', edit_team_path(@team) %> |
<%= link_to 'Back', teams_path %>
_flags.html.erb
<div>
<p>
<strong>Title:</strong>
<%= @flag.title %>
</p>
<p>
<strong>Description:</strong>
<%= @flag.description %>
</p>
<p>
<strong>Priority:</strong>
<%= @flag.priority %>
</p>
<p>
<strong>Status:</strong>
<%= @flag.status %>
</p>
<p>
<strong>Raisedby:</strong>
<%= @flag.raisedby.name %>
</p>
<p>
<strong>Assignedto:</strong>
<%= @flag.assignedto.name %>
</p>
<p>
<strong>Created By:</strong>
<%= @flag.user.name if @flag.user %>
<%= @flag.user.surname if @flag.user %>
</p>
<%= link_to 'Edit', edit_flag_path(@flag) %> |
<%= link_to 'Back', flags_path %>
</div>
Rails 服务器结果
Team Load (0.4ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering teams/show.html.erb within layouts/application
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."team_id" = ? [["team_id", 1]]
Rendered users/_user.html.erb (0.7ms)
Rendered users/_user.html.erb (0.1ms)
Flag Load (0.5ms) SELECT "flags".* FROM "flags" WHERE "flags"."assignedto_id" = ? AND (assignedto_id = "team_id") [["assignedto_id", 1]]
Flag Load (0.3ms) SELECT "flags".* FROM "flags" WHERE "flags"."raisedby_id" = ? AND (raisedby_id = "team_id") [["raisedby_id", 1]]
Rendered teams/show.html.erb within layouts/application (12.3ms)
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
Completed 200 OK in 138ms (Views: 129.8ms | ActiveRecord: 2.0ms)
它没有显示,因为你需要使用 flag
而不是 @flag,还优化了你的代码,请检查
<p>
<strong>Title:</strong>
<%= flag.title %>
</p>
<p>
<strong>Description:</strong>
<%= flag.description %>
</p>
<p>
<strong>Priority:</strong>
<%= flag.priority %>
</p>
<p>
<strong>Status:</strong>
<%= flag.status %>
</p>
<p>
<strong>Raisedby:</strong>
<%= flag.raisedby.name %>
</p>
<p>
<strong>Assignedto:</strong>
<%= flag.assignedto.name %>
</p>
<p>
<strong>Created By:</strong>
<%= flag.user&.name %>
<%= flag.user&.surname %>
</p>
<%= link_to 'Edit', edit_flag_path(flag) %> |
<%= link_to 'Back', flags_path %>
</div>