这个助手怎么样?

时间:2013-03-22 12:49:55

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

型号:

 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| -%>
             .......

当我运行rake路线时:

     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 个答案:

答案 0 :(得分:1)

您已将路线写为

resources :booth_marketing_messages do
     member do
       match :toggle_activation
     end
  end

因此,无论控制器中是否有任何操作,或者即使没有控制器,它也会创建基本路径,如索引,新建,创建,更新,销毁,编辑和显示。

但是当你去那个网址时,你会发现没有找到行动错误或控制器不存在。

因此,如果您想避免或不想使用默认路由,则只能使用路由中的路由 例如

resources :products, only: [:new]

这将仅为新动作和

创建路线
resources :products, except: [:new]

这将创建除新动作之外的所有路线

希望这能让你清楚