【问题标题】:Adding a new action to a restful resource向 RESTful 资源添加新操作
【发布时间】:2014-02-18 23:09:35
【问题描述】:

我需要在我的照片控制器中添加一个辅助显示操作。我已将其命名为全屏。

我的显示动作在页​​面中心显示一张照片,全屏动作将占据页面并尽可能大地显示照片。

我将如何在我的路由器中进行设置,以及在为 Photo Show 操作呈现的视图上的图像上的链接是什么样的?

只是想知道这一切是如何结合在一起的?

这是我的照片控制器:

class Gallery::PhotosController < Gallery::GalleryController

   def index
      @photos = Photo.all
   end

   def show
      @photo = Photo.find(params[:id])
      @album = Album.where(id: @photo.album_id).first
      @first_of_album = Photo.where(album_id: @album.id).first
      @last_of_album = Photo.where(album_id: @album.id).last

      respond_to do |format|
         format.html

         format.js
      end
   end

   def fullscreen
      @photo = Photo.find(params[:id])
      @album = Album.where(id: @photo.album_id).first
      @first_of_album = Photo.where(album_id: @album.id).first
      @last_of_album = Photo.where(album_id: @album.id).last
   end

end

还有我的路由器:

   namespace :gallery do
      resources :collections, only: [:index, :show] do
         resources :albums, only: [:index, :show] do
            resources :photos, only: :show
         end
      end
   end

我认为我不能使用链接助手,因为 Rails 不会;如果没有适用的路由,我不会生成一个,但我不确定如何在路由器端“插入”这个?

有什么想法吗?

【问题讨论】:

  • 您介意接受我的回答,这样我的帮助就不会让我扣分吗?

标签: ruby-on-rails hyperlink controller routes action


【解决方案1】:

这样做:

resources :photos, only: :show do
  member do
    get :fullscreen
  end
end

Here is the relevant doc.

【讨论】:

  • Code+ doc,我应该自己提交吗?
  • 谢谢。这很好用并且很有意义。我认为我会使用收集路线来处理需要可用于一组项目的东西?
猜你喜欢
  • 2019-01-15
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多