Ruby on Rails-路由到新页面

时间:2018-08-10 22:29:25

标签: ruby-on-rails

我想创建一个新的路由,该链接指向URL'../coins/:id/events/pending-events',但是我的新路由正导致'..coins /:coin_id / events /:event_id / pending-events”。我在这里做错什么了,我该如何解决?

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

2 个答案:

答案 0 :(得分:1)

只需将import Vue from 'vue'; Vue.component('latest-articles', () => import('./javascripts/components/LatestArticles.vue').then(m => m.default)); const app = new Vue({ el: '#app', components: { 'latest-articles': () => import('./javascripts/components/LatestArticles.vue').then(m => m.default) } }); 添加到您的路线,例如:

on: :collection

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

答案 1 :(得分:0)

我建议你这样做

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)更漂亮,而且打字错误更少

但是,那只是我。