我只想在“索引”中找到一个链接。查看打开相应的项目'图。
的routes.rb
library(grid)
library(gridExtra)
library(egg)
grid.arrange(gtable_cbind(gtable_frame(ggarrange(ggg1, ggg2, draw=F)),
gtable_frame(ggplotGrob(ggg3))),
left = textGrob("Left",rot=90),
bottom = textGrob("Bottom"))
projects_controller.rb
get '/project', to: 'projects#new'
get '/projects', to: 'projects#index'
get'/project/:id', to: 'projects#show'
post '/projects/new', to: 'projects#create'
#----Resources----
resources :users
resources :projects
resources :hazards
index.html.erb
def show
@project = Project.find(params[:id])
end
所以所有其他视图都正常工作,我可以手动输入项目/ id并查看视图。当我尝试链接而不是生成url项目/ id时,它生成project.id并指向项目#create path。
我知道这个问题的答案很简单,但如果我能搞清楚的话,我就会受到打击。解释会很好,所以我可以理解我的错误。
答案 0 :(得分:1)
其他答案似乎都很好。我还建议您使用标准resources
方法定义路线:
resources :projects
哪个会给你:
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
您可以根据需要/适当使用only:
或except:
修剪那些。
然后你应该能够做到:
link_to project.name project
您经常会将此视为以下任何一项:
link_to project.name project_path(id: project.id)
link_to project.name project_path(project.id)
link_to project.name project_path(project)
link_to project.name project
所有这些都是等效的。
哦,是的,正如Sebastian Palma所说,你现在正在传递id
的符号:
link_to project.name, project_path(:id)
在这种情况下,rails认为符号:id
是id
,当然不是。{/ p>
BTW,这些声明:
get '/project', to: 'projects#new'
get '/projects', to: 'projects#index'
get '/project/:id', to: 'projects#show'
post '/projects/new', to: 'projects#create'
包含一些非传统项目。具体来说,是第一,第三和第四个条目。
通常,在使用资源丰富的路由时,您希望对象名称(即project
)采用复数形式(即projects
)。如果你想手工制作这些,我相信它们会更像:
get '/projects', to: 'projects#index'
get '/projects/new', to: 'projects#new', as: :new_project
get '/projects/:id', to: 'projects#show', as: :project
post '/projects', to: 'projects#create'
然后会模仿由resources :projects
:
projects GET /projects(.:format) projects#index
new_project GET /projects/new(.:format) projects#new
project GET /projects/:id(.:format) projects#show
POST /projects(.:format) projects#create
你应该手绘你的路线 或 使用resources :projects
。你不应该同时做这两件事(因为看起来你可能正在做)。
答案 1 :(得分:0)
修改show的路线如下:
get '/projects/:id', to: 'projects#show'
但不是上面我建议你使用资源完整路线
即。 resources :projects