我有多个下拉项,例如登录,注销,注册,注册等。我希望它看起来像这样:
由于上述原因,我听错了做法:
/app/helpers/application_hepler.rb
module ApplicationHelper
def login_helper(css_class='', img_width=14)
unless current_user.is_a?(GuestUser)
<<~EOF.html_safe
<a href="#{destroy_user_session_path}" class="dropdown-item">
#{image_tag('logout.svg', width: "12px")}
Logout
</a>
EOF
else
link_to('Sign Up', new_user_registration_path, class: css_class) +
' '.html_safe +
link_to('Login', new_user_session_path, class: css_class)
end
end
上面的代码给出了所需的输出,但是注销按钮没有转到正确的路径。 href也缺少方法::delete
我想做的是将图像和文本包装在link_to()方法内,并对图像使用image_tag()。
我尝试了这些链接,但按照我的期望,答案并没有完全解决:
link_to image tag. how to add class to a tag
例如:
link_to("X", destroy_user_session_path, method: :delete, class: css_class).html_safe +
image_tag('logout.svg', width: "#{img_width}px") do
end
如何在ERB中添加link_to标签并将图像嵌套在文本旁边?
如果链接目标很难放入name参数中,则可以使用块,请参见下面的示例和生成的html
<%= link_to(@profile) do %>
<strong><%= @profile.name %></strong> -- <span>Check it out!</span>
<% end %>
# => <a href="/profiles/1">
<strong>David</strong> -- <span>Check it out!</span>
</a>
对于您上面的问题,我认为这可以解决
<%= link_to(destroy_user_session_path, method: :delete, class: css_class) do
image_tag('logout.svg', width: "#{img_width}px")
<% end %>
and this link可能可以帮助您更多细节