当 "views" 应设置为 RoR 中的条件时,条件 (if/else) 的语法?
Syntax for conditions(if/else) when "views" should be set as a condition in RoR?
我的导航栏在 header 下方...我想根据访问者所在的视图更改 header。
当 Rails 上的 Ruby 中将视图设置为条件时,条件 (if/else) 的语法是什么?
类似...
<% if index.html.erb %>
<%= image_tag("index_background.jpg", alt: "index background") %>
<% elsif about.html.erb %>
<%= image_tag("about_background.jpg", alt: "index background") %>
<% else %>
<%= image_tag("default_background.jpg", alt: "index background") %>
<% end %>
如果您有任何问题,请随时提问!提前致谢!
我想你可以使用 action_name
:
# Returns the name of the action this controller is processing.
attr_internal :action_name
这将为您提供用户实际所处的操作,因此您可以使用 switch 语句,最终这会比 if 条件更适合您必须进行的验证增长:
# to be added in a helper
image = case action_name
when index then 'index'
when about then 'about'
else
'default'
end
image_tag("#{image}_background.jpg", alt: 'index background')
注意 action_name
没有记录,但它的工作方式与 ActionController::Metal#controller_name
相同。
或者我认为你可以创建一个辅助方法,它使用 asset_path 来使用 action_name 获取你的资产路径,以防因为找不到它而引发异常, 然后你可以拯救它并显示默认图像:
# helper
module ApplicationHelper
def action_image_background
asset_path "#{action_name}_background"
rescue Sprockets::Rails::Helper::AssetNotFound
asset_path "default_background"
end
end
# view
<%= image_tag action_image_background %>
这些条件在语法上是正确的,您只需要理清逻辑即可。如果那些 index.html.erb
和 about.html.erb
要检查当前正在呈现的文件,您应该在控制器中的某个变量中实例化它们的名称以进行比较,或者使用 :controller
和 :action
参数。
也许您需要这样的东西:
<% if params[:action] == 'index' %>
<%= image_tag("index_background.jpg", alt: "index background") %>
<% elsif params[:action] == 'about' %>
<%= image_tag("about_background.jpg", alt: "index background") %>
<% else %>
<%= image_tag("default_background.jpg", alt: "index background") %>
<% end %>
视图中的逻辑过多,将其移至辅助方法:
def image_background
img = (params[:action] || 'default') + "_background.jpg"
image_tag img, alt: "index background"
end
我的导航栏在 header 下方...我想根据访问者所在的视图更改 header。
当 Rails 上的 Ruby 中将视图设置为条件时,条件 (if/else) 的语法是什么?
类似...
<% if index.html.erb %>
<%= image_tag("index_background.jpg", alt: "index background") %>
<% elsif about.html.erb %>
<%= image_tag("about_background.jpg", alt: "index background") %>
<% else %>
<%= image_tag("default_background.jpg", alt: "index background") %>
<% end %>
如果您有任何问题,请随时提问!提前致谢!
我想你可以使用 action_name
:
# Returns the name of the action this controller is processing.
attr_internal :action_name
这将为您提供用户实际所处的操作,因此您可以使用 switch 语句,最终这会比 if 条件更适合您必须进行的验证增长:
# to be added in a helper
image = case action_name
when index then 'index'
when about then 'about'
else
'default'
end
image_tag("#{image}_background.jpg", alt: 'index background')
注意 action_name
没有记录,但它的工作方式与 ActionController::Metal#controller_name
相同。
或者我认为你可以创建一个辅助方法,它使用 asset_path 来使用 action_name 获取你的资产路径,以防因为找不到它而引发异常, 然后你可以拯救它并显示默认图像:
# helper
module ApplicationHelper
def action_image_background
asset_path "#{action_name}_background"
rescue Sprockets::Rails::Helper::AssetNotFound
asset_path "default_background"
end
end
# view
<%= image_tag action_image_background %>
这些条件在语法上是正确的,您只需要理清逻辑即可。如果那些 index.html.erb
和 about.html.erb
要检查当前正在呈现的文件,您应该在控制器中的某个变量中实例化它们的名称以进行比较,或者使用 :controller
和 :action
参数。
也许您需要这样的东西:
<% if params[:action] == 'index' %>
<%= image_tag("index_background.jpg", alt: "index background") %>
<% elsif params[:action] == 'about' %>
<%= image_tag("about_background.jpg", alt: "index background") %>
<% else %>
<%= image_tag("default_background.jpg", alt: "index background") %>
<% end %>
视图中的逻辑过多,将其移至辅助方法:
def image_background
img = (params[:action] || 'default') + "_background.jpg"
image_tag img, alt: "index background"
end