为什么Rails无法识别project_path [英] Why does Rails not recognise project_path

查看:65
本文介绍了为什么Rails无法识别project_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮忙...这让我发疯。

I wonder if anyone can help with this...its driving me crazy.

首先,我对这一切仍然很陌生。抱歉,如果我弄错了一些术语。我已经完成了一些教程,现在正在我的第一个应用程序上工作,而没有任何指导性的演练...进度缓慢:)

Firstly I'm still pretty new to all of this. So apologies if I get some terminology wrong. I have completed some tutorials and am now working on my first app without any guided walkthroughs... its slow going :)

我有两组页面,一组在哪里我应该显示一个侧边栏和一个不需要它的组。以下是我的路线...

I have two groups of pages, one group where I should show a sidebar and one group that does not need it. Below are my routes...

Rails.application.routes.draw do

#----Pages----
root 'pages#home'
get '/home', to: 'pages#home'
get '/contact', to: 'pages#contact'

#----Users----
get '/app', to: 'users#app'
get 'users/new', to: redirect('/signup')
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'

#----Sessions----
get      '/sessions/new', to: 'sessions#new'
get    '/login',   to: 'sessions#new'
post   '/login',   to: 'sessions#create'
delete '/logout',  to: 'sessions#destroy'


#----Projects----


#----Hazards----


#----Resources----
resources :users
resources :projects
resources :hazards
end

I在我的标头中有以下代码可以正常工作...

I have the following code in my header which works fine...

<% if current_page?(app_path) ||
            current_page?(users_path) ||
            current_page?(projects_path) ||
            current_page?(hazards_path)
    <%= render 'layouts/sidebar' %>
<% end %> 

它为上面列出的那些路径(而不是其他路径)提供了侧边栏。但是...我想从'project_path'中添加到以上页面的列表中,以便/ projects / 6等。为实现这一点,我在以下代码中添加了以下代码...

it supplies a sidebar to those paths listed above and not to the others. However... I wish to add to the list above pages from the 'project_path' so /projects/6 etc. To achieve this I added the following line to the code above...

current_page?(project_path(:id)

导致...

<% if current_page?(app_path) ||
            current_page?(users_path) ||
            current_page?(projects_path) ||
            current_page?(hazards_path) ||
            current_page?(project_path(:id)) %>
    <%= render 'layouts/sidebar' %>

<% end %>

一旦添加应用程序加载正常,但边栏未添加到项目页面,因此我尝试了如下替代方法...

Once added the app loads fine but the sidebar is NOT added to the project page. So I tried an alternative method as follows...

current_page?(project_path(@project))

此修改后,项目页面现在显示侧栏正确,但是...所有其他没有侧栏的页面无法加载机智h以下错误...

After this amendment the project page now shows the sidebar correctly but... all other pages that do not have the sidebar fail to load with the following error...

No route matches {:action=>"show", :controller=>"projects", :id=>nil}    missing required keys: [:id]

我是尽我的智慧...我也尝试过

I'm at my wits end... I have also tried

current_page?(project_path(params.permit))
Current_page?(project_path(project))
current_page?(project_path(project(params[:id])))

有人可以让我摆脱痛苦吗?那不是夸张...纯粹的痛苦。

Can someone please put me out of my misery... thats not an exaggeration... pure misery.

非常感谢。

推荐答案

好吧,其他页面没有 @project 。也许,您可以这样做:

Well, the other pages don't have a @project. Perhaps, you could do something like this:

current_page?(projects_path) ||
@project && current_page?(project_path(@project)) ||
...

这篇关于为什么Rails无法识别project_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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