2013-03-22 44 views
0

型號:這個幫手是如何工作的?

Group: 
has_many :booth_marketing_messages, :dependent => :destroy 

Booth Marketing Message: 
belongs_to :group 

路線:

resources :groups do 
     member do 
     get :get_group_links 
     get :booth_marketing_messages 
     end 
     resources :booth_marketing_messages do 
     member do 
      match :toggle_activation 
     end 
     end 
    end 

爲了創建一個新的展臺營銷信息我有一個觀點:

<% form_for :asset, :url => (defined?(msg) ? group_booth_marketing_message_path(msg) :   
     group_booth_marketing_messages_path), :html => { :multipart => true, :method => 
     (defined?(msg) && msg ? :put : :post) } do |f| -%> 
      ....... 

當我運行耙路線:

 booth_marketing_messages_group GET /groups/:id/booth_marketing_messages(.:format)           
    {:action=>"booth_marketing_messages", :controller=>"groups"} 

     group_booth_marketing_messages GET 
    /groups/:group_id/booth_marketing_messages(.:format)         
    {:action=>"index", :controller=>"booth_marketing_messages"} 

但我的攤位營銷消息控制器沒有索引操作。是的,這條路線不會失敗,那是怎麼回事?

回答

1

你已經寫了路線爲

resources :booth_marketing_messages do 
    member do 
     match :toggle_activation 
    end 
    end 

所以它會創建基本路線爲指標,新,創建,更新,銷燬,編輯和無論顯示的有在控制器中提到的任何動作或即使有沒有控制器。

但是當你去那個URL你會得到錯誤的行動沒有找到或控制器不存在。

所以,如果你想避免或不想使用默認路由,你只能使用,除非在路由 例如。

resources :products, only: [:new] 

這將僅用於新的行動創造路線和

resources :products, except: [:new] 

這將創建除了新行動的所有路由

希望這是你清楚

+0

我明白你是什麼sayign但是「但是當你去那個網址時,你會得到錯誤的行爲沒有找到或控制器不存在。」 - 多數民衆贊成的事情,我不必爲BoothMarketingMessagesController索引行動/索引視圖,但我在'新'行動的形式中提到的網址沒有拋出任何錯誤...我想知道爲什麼它不是拋出一個錯誤? – user1865578 2013-03-22 13:26:11

+0

意思是如果你在瀏覽器中輸入該網址你沒有得到像「未知動作」的錯誤? 那麼你會得到什麼?任何頁面? – jbmyid 2013-03-22 13:33:16

+0

是的,它不拋出任何錯誤而是被路由到booth_marketing_messages_group_path – user1865578 2013-03-22 13:42:16