Ruby on Rails:如果是当前页面?是主页,不显示表格 [英] Ruby on Rails: if current page? is homepage, don't show form

查看:43
本文介绍了Ruby on Rails:如果是当前页面?是主页,不显示表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想显示表单,但前提是当前页面不是主页

这是我目前所拥有的...

我设置了路线:

root 'projects#index'

我的观点:

<% if !current_page?(url_for(:controller => 'projects', :action => 'index')) %>展示一些东西<%结束%>

这不会显示 url 是否为 localhost:3000/projects

但它显示它的 localhost:3000

所以我需要以某种方式确保如果它是主页,它就不会显示.另外,我有主页的搜索参数,但我仍然不想显示它是否像 localhost:3000/projects?search=blahblahblah

解决方案

使用 root_path 助手:

<% 除非 current_page?(root_path) %>展示一些东西<%结束%>

<块引用>

这不会显示 url 是否为 localhost:3000/projects 但它显示如果它的 localhost:3000

或:

<% 除非 current_page?('/') ||current_page?('/projects') %># '/' 与 root_path 相同展示一些东西<%结束%>

另外,根据documentation,不需要url_for 方法:

current_page?(controller: 'projects', action: 'index')

I want to not show a form, but only if current page is NOT the home page

This is what I have so far...

I have my route setup:

root 'projects#index'

My view:

<% if !current_page?(url_for(:controller => 'projects', :action => 'index')) %>
  show some stuff
<% end %>

This doesn't show if the url is localhost:3000/projects

But it shows if its localhost:3000

So I need to somehow make sure that if its the homepage, it won't show. Also, I have search parameters for the home page, and I still don't want to show if its like localhost:3000/projects?search=blahblahblah

解决方案

Use the root_path helper:

<% unless current_page?(root_path) %>
  show some stuff
<% end %>

This doesn't show if the url is localhost:3000/projects But it shows if its localhost:3000

or:

<% unless current_page?('/') || current_page?('/projects') %>
   # '/' the same as root_path
   show some stuff
<% end %>

Also, according the documentation, no need url_for method:

current_page?(controller: 'projects', action: 'index')

这篇关于Ruby on Rails:如果是当前页面?是主页,不显示表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆