2016-03-03 72 views
0

當我嘗試創建新的數據庫條目或更新當前數據時,出現路由錯誤。Rails 4名稱空間

錯誤:沒有路由匹配[POST] 「/酒吧」

的routes.rb:

resources :people, except: [:show] do 
    resources :pubs, except: [:create, :new, :edit, :destroy] 
end 

resources :articles 
resources :pubs, except: [:create, :new, :edit, :destroy] 


namespace :sekret do 
    resources :people do 
     resources :pubs 
    end 
end 

sekret/pubs_controller

class Sekret::PubsController < SekretController 

def index 
    @pubs = Pub.all 
end 

def show 
    @pub = Pub.find(params[:id]) 
end 

def new 
    @person = Person.find(params[:person_id]) 
    @pub = @person.pubs.new 
end 

def create 

    @pub = Pub.new(pub_params) 

    if @pub.save 
     flash[:notice] = "Article created successfully!" 
     redirect_to sekret_person_pub_path(@pub) 
    else 
     render :new, status: :unprocessable_entity 
    end 
end 

def edit 
    @pub = Pub.find(params[:id]) 
end 

def update 
    @pub = Pub.find(params[:id]) 

    if @pub.update(pub_params) 
     redirect_to sekret_person_pub_path(@pub) 
    else 
     render :edit, status: :unprocessable_entity 
    end 
end 

def destroy 
    pub = Pub.find(params[:id]) 
    pub.destroy 
    redirect_to sekret_people_path 
end 




private 
def pub_params 
    params.require(:pub).permit(
     :pubmed_id, :journal, :pages, :date, :type, :link, :authors, 
     :title, :notes, :auth_id, :person_id) 
end 
end 

通過所有這些設置的打算後,當,我允許非命名空間的pubs解析編輯,更新等,更新過程順利完成。一旦我將這些函數限制在受密碼保護的命名空間中,我就會收到路由錯誤。通過路線解析後,我可以看到sekret_person_pub_path在那裏列出。我想我在某個地方失去了一些東西。

耙路線:

pubs#index 
          pub GET /pubs/:id(.:format)        pubs#show 
           PATCH /pubs/:id(.:format)        pubs#update 
           PUT /pubs/:id(.:format)        pubs#update 
      sekret_person_pubs GET /sekret/people/:person_id/pubs(.:format)   sekret/pubs#index 
           POST /sekret/people/:person_id/pubs(.:format)   sekret/pubs#create 
      new_sekret_person_pub GET /sekret/people/:person_id/pubs/new(.:format)  sekret/pubs#new 
     edit_sekret_person_pub GET /sekret/people/:person_id/pubs/:id/edit(.:format) sekret/pubs#edit 
       sekret_person_pub GET /sekret/people/:person_id/pubs/:id(.:format)  sekret/pubs#show 
           PATCH /sekret/people/:person_id/pubs/:id(.:format)  sekret/pubs#update 
           PUT /sekret/people/:person_id/pubs/:id(.:format)  sekret/pubs#update 
           DELETE /sekret/people/:person_id/pubs/:id(.:format)  sekret/pubs#destroy 
        sekret_people GET /sekret/people(.:format) 
+0

你什麼時候看到'''耙路線'''? – SacWebDeveloper

+0

將路線添加到原始帖子。由於我對所有這些都是新手,如果路線是:/sekret/people/:person_id/pubs/:id(.:format)我是否必須找到一種方法將person_id和:id放在那裏或將會它根據它正在更新的頁面生成? – lostrennie

回答

0

使用resources :pubs, except: [:create, :new, :edit, :destroy],你是防止提供POST /pubs路徑產生。

namespace和嵌套resources將生成一個URL POST sekret/people/:person_id/pubs

在你的控制器中,你應該創建Pub作爲關聯對象。

def create 
    person = Person.find(params[:person_id]) 
    @pub = person.pubs.new(pub_params) 

    if @pub.save 
    flash[:notice] = "Article created successfully!" 
    redirect_to sekret_person_pub_path(@pub) 
    else 
    render :new, status: :unprocessable_entity 
    end 
end 

如果要限制訪問create方法,你可以使用一個授權庫,如權威人士在這種情況下,你會建立一個政策,限制誰可以做什麼。

https://github.com/elabs/pundit

0

您在路線錯過了,因爲導軌形式不使用正確的路線時的命名空間,所以你必須手動指定它們

<%= form for @pub, url: sekret_person_pubs_path do |f| %> 

讓表單知道哪些路線後,如果不指定URL,軌道將使用url: person_pubs_path幕後

編輯:忘了補充_path

+0

這是如何工作,如果我使用部分?該頁面調用存根:<%= render'form',pub:@pub%>。或者它會替換存根開啓器<%= form_for(pub)do | f | %>>? – lostrennie

+0

你可以在表單partial((pub,url:sekret_person_pubs_path)do | f | –

相關問題