2017-02-09 64 views
0

所以我真的是新的鐵路和看了幾個教程後,我決定去做一個基本的網站。我目前正處於開發階段,但有一個障礙,我似乎無法得到link_to,因爲我在加載頁面時收到No route matches錯誤。Rails - 在路由中的new_projects_path,但得到'沒有路由匹配'的錯誤

這是我的索引頁代碼

<h1>Projects#index</h1> 
<%= link_to "Create new project", action: new_project_path %> 

我碰到我的終端new_project_path當我輸入命令rake routes這裏是返回的一切:

 Prefix Verb URI Pattern     Controller#Action 

     root GET /       projects#index 
    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 

和這裏來自projects_controller文件的代碼:

class ProjectsController < ApplicationController 
    def index 

    end 

    def create 
    @project = Project.create(project_params) 
    if(@project.save) 
     redirect_to root_url, :notice => "Project added" 
    else 
     render "new" 
    end 
    end 

    def new 
    @project = Project.new 
    end 


    private 

    def project_params 
    params.require(:project).permit(:title, :desc, :dates) 
    end 

end 

我只是想知道我哪裏出錯了,因爲這已經困擾了幾個小時了。任何幫助,你可以給予不勝感激! :) 我檢查了文檔,似乎無法找到我的錯誤,所以我真的不知道。

回答

1

你可能想這樣的:

<%= link_to "Create new project", new_project_path %> 

做過什麼,在這裏:

<%= link_to "Create new project", action: new_project_path %> 

說讓action PARAM等於"/projects/new"這是不是你畢竟是什麼。

+0

這就是它!謝謝:)對不起,這是一個簡單的修復! –

0

您可以使用以下任何方法:

<%= link_to "Create new project", new_project_path %> 

OR

<%= link_to "Create new project", controller: "projects", action: "new" %> 

OR

<%= link_to "Create new project", "/projects/new" %> 
相關問題