So I'm really new to rails and after watching several tutorials I decided to have a go at making a basic site. I'm at the start of development at the moment, but there's a snag, I can't seem to get link_to
to work as I'm getting a No route matches
error returned when I load the page.
This is my code for the index page
<h1>Projects#index</h1>
<%= link_to "Create new project", action: new_project_path %>
I'm getting new_project_path
from my terminal when I enter the command rake routes
here is everything that is returned:
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
and here is the code from the projects_controller
file:
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
I'm just wondering where I'm going wrong because this has had e confused for a couple of hours now. Any help you can give would be greatly appreciated! :) I've checked the docs and can't seem to find my error so I really don't know.
You probably want this:
<%= link_to "Create new project", new_project_path %>
What you did here:
<%= link_to "Create new project", action: new_project_path %>
Says to make the action
param equal to "/projects/new"
which isn't what you're after all.
You can use any below methods:
<%= 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" %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.