2016-04-08 153 views
1

我開始學習Ruby on Rails,到目前爲止一切進展順利,但最終出現了一個我無法解決的問題。當我編輯表格上的信息,我點擊給它提出了我下面的錯誤更新(對不起,我的英語,我不是在語言流利):路由錯誤沒有路由匹配[POST]

Routing Error 
No route matches [POST] "/grupos/14/edit" 

Rails.root: C:/Ruby/Projetos/florarails 

Application Trace | Framework Trace | Full Trace 
Routes 

outes match in priority from top to bottom 

Helper   HTTP Verb  Path     Controller#Action 

grupos_path GET  /grupos(.:format)   grupos#index 
       POST  /grupos(.:format)   grupos#create new_grupo_path 
       GET  /grupos/new(.:format)  grupos#new 
edit_grupo_path 
       GET  /grupos/:id/edit(.:format) grupos#edit 
grupo_path 
       GET  /grupos/:id(.:format)  grupos#show 
       PATCH  /grupos/:id(.:format)  grupos#update 
       PUT  /grupos/:id(.:format)  grupos#update 
       DELETE  /grupos/:id(.:format)  grupos#destroy 

這裏是我的代碼控制器:

class GruposController < ApplicationController 
    def index 
    @grupos = Grupo.all 
    end 

    def show 
    @grupo = Grupo.find(params[:id]) 
    end 

    def new 
    @grupo = Grupo.new 
    end 

    def create 
    @grupo = Grupo.new(user_params) 
    if @grupo.save 
     flash[:aviso] = 'Grupo salvo com sucesso' 
    else 
     flash[:erro] = 'Erro ao salvar grupo' 
    end 
    redirect_to (@grupo) 
    end 

    private 

    def user_params 
    params.require(:grupo).permit(:descricao) 
    end 

    def edit 
    @grupo = Grupo.find(params[:id]) 
    end 

    def update 
    @grupo = Grupo.find(params[:id]) 
    if @grupo.update_attributes(params[:grupo]) 
     flash[:aviso] = 'Grupo salvo com sucesso' 
    end 
    redirect_to grupos_path 
    end 

    def destroy 
    @grupo = Grupo.find(params[:id]) 
    @grupo.destroy 
    flash[:info] = "Grupo excluido com sucesso" 
    redirect_to(grupos_path) 
    end 
end 

這裏是我的視圖代碼:

<%= form_for :grupo do |f| %> 
    <p>Edição de Grupos</p> 
    <%= f.label :descricao, "Descrição:" %>: 
    <%= f.text_field :descricao, :size => 40 %> 
    <%= f.submit "Alterar Dados" %> 
<% end %> 

這裏是文件內容的routes.rb:

Rails.application.routes.draw do 

    resources :grupos 
    match 'grupos/:id', controller: 'grupos', action: 'show', via: 'get' 
    match 'grupos/:id/edit', controller: 'grupos', action: 'edit', via: 'get' 
    match 'grupos/:id/edit', controller: 'grupos', action: 'update', via: 'post' #(When this line is added another error is displayed on the screen >> "Unknown action The action 'update' could not be found for GruposController") 

end 

感謝您的關注。

回答

0

你的表單字段試試這個:

<%= form_for :grupo, url: :grupo_path do |f| %> 
    <p>Edição de Grupos</p> 
    <%= f.label :descricao, "Descrição:" %>: 
    <%= f.text_field :descricao, :size => 40 %> 
    <%= f.submit "Alterar Dados" %> 
<% end %> 

另外,你需要對路由match線。 resources :grupos將爲您覆蓋。

刪除:

match 'grupos/:id', controller: 'grupos', action: 'show', via: 'get' 
match 'grupos/:id/edit', controller: 'grupos', action: 'edit', via: 'get' 
match 'grupos/:id/edit', controller: 'grupos', action: 'update', via: 'post' 
+0

按照您編寫的步驟進行操作。非常感謝!!! –

+0

@LucasRuy我很高興,'耙路線'將有助於更容易理解生成路線文件的內容。 – 7urkm3n

0

首先,POST行爲應該保留用於對象創建,而不是更新。使用PUTPATCH進行更新。

而且,在你的控制器(editupdatedestroy)關鍵行動是private這應該是公開的,以便能夠進行路由。這是上一節中錯誤的來源。

簡而言之,您的大部分問題都可以簡單地使用resources :groupos來解決。這將產生一致的路線:

 grupos GET  /grupos(.:format)   grupos#index 
      POST  /grupos(.:format)   grupos#create 
    new_grupo GET  /grupos/new(.:format)  grupos#new 
    edit_grupo GET  /grupos/:id/edit(.:format) grupos#edit 
     grupo GET  /grupos/:id(.:format)  grupos#show 
      PATCH  /grupos/:id(.:format)  grupos#update 
      PUT  /grupos/:id(.:format)  grupos#update 
      DELETE  /grupos/:id(.:format)  grupos#destro 

最後一點,你應該不是form_for :gropo用戶form_for @grupo。這將允許從它們的匹配屬性來動態填充在@grupo

+0

感謝您的澄清。但我試圖只使用資源:grupos並沒有工作。 –

相關問題