簡體   English   中英

Ruby on Rails-路由到新頁面

[英]Ruby on Rails - routing to a new page

我想創建一個新的路由,該鏈接指向url'../coins/:id/events/pending-events',但是我的新路由正導致'..coins /:coin_id / events /:event_id / pending-事件”。 我在這里做錯什么,該如何解決?

的routes.rb

resources :coins do
  ...
  resources :events do
    get 'pending-events', to: 'events#pending'
    member do
      put "like", to: "events#upvote"
      put "dislike", to: "events#downvote"
    end
  end
  ...
end

event_controller.rb

...
def pending
  @events = Event.where(coin_id: @coin.id).order("created_at DESC")
end
...

只需添加on: :collection到您的路線,例如:

  resources :coins do
    ...
    resources :events do
      get 'pending-events', to: 'events#pending', on: :collection
      member do
        put "like", to: "events#upvote"
        put "dislike", to: "events#downvote"
      end
    end
    ...
  end

更多信息: https : //guides.rubyonrails.org/routing.html#adding-collection-routes

我建議你這樣做:

resources :coins do 
  resources :events do
    collection do 
      get :pending
    end
    member do
      put "like", to: "events#upvote"
      put "dislike", to: "events#downvote"
    end
  end
end

這會給你:

pending_coin_events GET    /coins/:coin_id/events/pending(.:format)             events#pending
    like_coin_event PUT    /coins/:coin_id/events/:id/like(.:format)            events#upvote
 dislike_coin_event PUT    /coins/:coin_id/events/:id/dislike(.:format)         events#downvote
        coin_events GET    /coins/:coin_id/events(.:format)                     events#index
                    POST   /coins/:coin_id/events(.:format)                     events#create
     new_coin_event GET    /coins/:coin_id/events/new(.:format)                 events#new
    edit_coin_event GET    /coins/:coin_id/events/:id/edit(.:format)            events#edit
         coin_event GET    /coins/:coin_id/events/:id(.:format)                 events#show
                    PATCH  /coins/:coin_id/events/:id(.:format)                 events#update
                    PUT    /coins/:coin_id/events/:id(.:format)                 events#update
                    DELETE /coins/:coin_id/events/:id(.:format)                 events#destroy
              coins GET    /coins(.:format)                                     coins#index
                    POST   /coins(.:format)                                     coins#create
           new_coin GET    /coins/new(.:format)                                 coins#new
          edit_coin GET    /coins/:id/edit(.:format)                            coins#edit
               coin GET    /coins/:id(.:format)                                 coins#show
                    PATCH  /coins/:id(.:format)                                 coins#update
                    PUT    /coins/:id(.:format)                                 coins#update
                    DELETE /coins/:id(.:format)                                 coins#destroy

無需指定to:並且pending_coin_events_path讀取效果很好。

就個人而言,我會這樣做:

resources :coins do 
  resources :events do
    collection do 
      get :pending
    end
    member do
      put :upvote
      put :downvote
    end
  end
end

這會給你:

pending_coin_events GET    /coins/:coin_id/events/pending(.:format)             events#pending
  upvote_coin_event PUT    /coins/:coin_id/events/:id/upvote(.:format)          events#upvote
downvote_coin_event PUT    /coins/:coin_id/events/:id/downvote(.:format)        events#downvote
        coin_events GET    /coins/:coin_id/events(.:format)                     events#index
                    POST   /coins/:coin_id/events(.:format)                     events#create
     new_coin_event GET    /coins/:coin_id/events/new(.:format)                 events#new
    edit_coin_event GET    /coins/:coin_id/events/:id/edit(.:format)            events#edit
         coin_event GET    /coins/:coin_id/events/:id(.:format)                 events#show
                    PATCH  /coins/:coin_id/events/:id(.:format)                 events#update
                    PUT    /coins/:coin_id/events/:id(.:format)                 events#update
                    DELETE /coins/:coin_id/events/:id(.:format)                 events#destroy
              coins GET    /coins(.:format)                                     coins#index
                    POST   /coins(.:format)                                     coins#create
           new_coin GET    /coins/new(.:format)                                 coins#new
          edit_coin GET    /coins/:id/edit(.:format)                            coins#edit
               coin GET    /coins/:id(.:format)                                 coins#show
                    PATCH  /coins/:id(.:format)                                 coins#update
                    PUT    /coins/:id(.:format)                                 coins#update
                    DELETE /coins/:id(.:format)                                 coins#destroy

我更喜歡那是因為:

  1. 您打字的次數減少了
  2. 您的動作和路徑是平行的
  3. 符號(IMO)更漂亮,且打字錯誤更少

但是,那只是我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM