gpt4 book ai didi

css - 将学区 Logo 与 URL 学区 ID 相匹配

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:46 24 4
gpt4 key购买 nike

目前,我已经创建了一个包含 5 个学区的应用程序。一切正常,因为每个学区在 app/assets/images 中都有自己的学校 Logo 。

如果当前 URL 包含每个学区的 ID,我想显示每个学区的 Logo 。

Ex. 
###These URLs should all display the Logo for District 1
districts/1/students
districts/1
districts/1/faculties

目前,我找到了一种在 URL 与 Districts ID 匹配时显示 Logo 的方法。但这是非常低效的,因为我添加了除 StaticPages Controller 之外的更多 Controller 。

EX. 
If I have a url like this /events/1
I will get an error unless I add the Events Controller to the conditional

如果 URL 包含学区 ID,我如何更改代码以便显示适当的学区 Logo ,而不需要将所有其他 Controller 添加到条件中?

观点

 ###StaticPages Controller
<% if params[:controller] == 'static_pages' %>
Welcoming Practices
<% elsif @districts %>
Welcoming Practices

###Checks district ID
<% elsif @district.id == 1 %>
<%= link_to "#{image_tag("bonsall_logo.png")} Bonsall Unified District".html_safe, "1" %>
<% elsif @district.id == 2 %>
<%= link_to "#{image_tag("chulavista_logo.png")} Chula Vista School District".html_safe %>
<% elsif @district.id == 3 %>
<%= link_to "#{image_tag("fallbrook_logo.gif")} Fallbrook School District".html_safe %>
<% elsif @district.id == 4 %>
<%= link_to "#{image_tag("oceanside_logo.gif")} Oceanside School District".html_safe %>
<% elsif @district.id == 5 %>
<%= link_to "#{image_tag("temecula_logo.png")} Temecula Valley School District".html_safe %>
<% end %>

最佳答案

您有几个选择..其中两个是:

  • 您可以将路线设置为 /:controller/:district_id 格式,而不是从 @district.id 选择图像,您可以设置它基于params[:district_id]
  • 您也可以简单地将文件重命名为 district_#.jpg 并包含基于学区的文件。
  • 您还可以确保始终将@district 设置为当前地区

PS - 你真的应该将文件路径存储在 District 模型/表中,这样你就不必从硬编码值创建 image_tags。

关于css - 将学区 Logo 与 URL 学区 ID 相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25214492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com