当脚手架时,Rails 4路由错误

时间:2014-07-16 10:35:11

标签: ruby-on-rails routing scaffolding

执行rails g scaffold Project name:string后,我在创建新项目时遇到路由错误。

我的步骤:
1)在/projects上,我点击“新项目” 2)填写项目详细信息,然后单击“创建项目” 在这一步中,表格似乎是正确的:
form accept-charset="UTF-8" action="/projects" class="new_project" id="new_project" method="post"
3)我收到以下错误(路由错误):No route matches [POST] "/projects/new"

鉴于:
当我重新加载/projects/new页面时,项目的创建工作正常

这有什么原因吗?

1 个答案:

答案 0 :(得分:0)

以下是我拥有的文件(我使用脚手架创建了大部分文件)

app / models / project.rb:

class Project < ActiveRecord::Base
end


app / controllers / projects_controller.rb :(我这里只展示我们感兴趣的方法)

  class ProjectsController < ApplicationController

  before_filter :authenticate_user!, :except => [:show, :index] 
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  # GET /projects/new
  def new
    @project = Project.new
  end

  # GET /projects/1/edit
  def edit
  end

  # POST /projects
  # POST /projects.json
  def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render action: 'show', status: :created, location: @project }
      else
        format.html { render action: 'new' }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_project
      @project = Project.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:name)
    end
end

和config / routes.rb:

Testproject::Application.routes.draw do
  devise_for :users
  resources :projects
  root 'projects#index'
end

感谢您的帮助