路由错误未初始化的常量

时间:2014-01-08 06:03:56

标签: ruby-on-rails ruby ruby-on-rails-3

我正在努力学习RoR。 我的控制器是

class SectionController < ApplicationController
  def new
    if request.post?
      u=SectionMst.new( :section_name => params[:section_name])
      u.save
      redirect_to("/section")
    else
      render 
    end
  end

  def index
    @sections = SectionMst.all
  end

  def destroy
    u=SectionMst.destroy(params[:id])
    u.save
    redirect_to("/section")
  end

  def edit
    @user = SectionMst.find(params[:id])
  end
end

和index.html.erb是

<%= link_to "Edit", edit_section_path(section.id), method: :edit %>

rake routes是

  section_new  POST   /section/new(.:format)      section#new
               POST   /section/:id/edit(.:format) section/:id#edit
 section_index GET    /section(.:format)          section#index
               POST   /section(.:format)          section#create
 new_section   GET    /section/new(.:format)      section#new
 edit_section  GET    /section/:id/edit(.:format) section#edit
      section  GET    /section/:id(.:format)      section#show
               PUT    /section/:id(.:format)      section#update
               DELETE /section/:id(.:format)      section#destroy

routes.rb是

post "section/new"
post "section/:id/edit"
resources :section

我得到了 路由错误 未初始化的常数部分

如果我删除第二行routes.rb 然后我明白了 路由错误 没有路线匹配[POST]“/ section / 3 / edit”

无法得到原因???

3 个答案:

答案 0 :(得分:4)

  1. 摆脱routes.rb中的第一行和第二行。他们是多余的。 resources会自动创建这些行。

  2. resources :section应写为resources :sections。请注意,它是复数。

  3. index.html.erb中,您根本不应提及method:。它会自动设置,并且:edit方法不存在。方法是指放入或获取或删除,但通常不必提及它。

答案 1 :(得分:2)

routes.rb

中不需要这一行
post "section/new"
post "section/:id/edit"

将第三行更改为:

resources :sections #plural

如果删除它们,可以使用

点击编辑视图
<%= link_to "Edit", edit_section_path(section.id), method: :edit %>

会在section/3/editGET点击您的应用。

edit.html.erb中,您可以使用字段来捕获修改内容,然后PUT/section/3

请注意,RAILS使用HTTP谓词来定义CRUD操作。参考here

答案 2 :(得分:0)

检查控制器的文件名,因为它应该是复数。它应该与类名匹配。因此,您应该将app/controllers/section_controller.rb重命名为app/controllers/sections_controller.rb

相关问题