如何将模型链接到控制器操作

时间:2015-02-05 17:13:36

标签: ruby-on-rails activemodel

我的项目中有一个 Slider 模型,它与产品,制造商,文章等其他模型有很多多态关联。

因此,当我使用其中一个模型的' show' 动作时,我也会显示相关的Slider。没关系。但有时我需要使用' index' 操作来显示滑块

将某些滑块链接到动作而不是其他模型的最佳方法是什么?

更新

路由

resources :products, :articles, :industries, :manufacturers, only: [:index, :show]

产品控制器

class ProductsController < ApplicationController

 load_resource

 # GET /products
 # GET /products.json
 def index
    @catalog = Product.by_type_and_manufacturer
 end

 # GET /products/1
 # GET /products/1.json
 def show
     @page_slider = @product.slider
 end
 end

所以在&#39; show&#39;动作我只是使用product.slider来获取相关的Slider实例。但我想通过索引操作为所有产品显示另一个滑块。

1 个答案:

答案 0 :(得分:0)

在这种情况下,您尝试做的事情是不可能的。您无法创建与控制器操作的关系。您需要做的是链接关系的控制器操作,而不是尝试创建与控制器操作的关系。 model只能与另一个model相关联(您不能has_many indexshowdeleteetc ...) - 其他单词,调用关系的数据,并在视图中链接到该关系的controller action

示例:

#Models:

class Page < ActiveRecord::Base
    has_many :sliders
end

class Slider < ActiveRecord::Base
    belongs_to :page
end

#Controllers

class PagesController < ApplicationController

    def index
        @pages = Page.all # lists all pages
    end

    def show
        @page = Page.find(params[:id]) # simplified, this will probably make use of strong params in your actual code
        @sliders = @page.sliders # all sliders related to the page
    end

   # if you would like to show a page that just has all sliders for a specific page and not the page itself...

   def show_page_sliders # you will have to create a route and view for this manually
        @page = Page.find(params[:id]) # simplified, this will probably make use of strong params in your actual code
        @sliders = @page.sliders # all sliders related to the page
        # note that this controller action is identical to the show action, because the data we're going to need is the same- the difference comes in the view
   end
end

class SlidersController < ApplicationController

    def index
        @sliders = Slider.all
    end

    def show
        @slider = Slider.find(params[:id])
    end

end


# Views
  # page#index
    <% @pages.each do |p| %>
      ...
      page listing code goes here. if you want to list the sliders for each page on the index...
    <% p.sliders.each do |s| %>
      ...
      individual slider info goes here
      ...
    <% end %>
    ...
   <% end %>

    # pages#show

   <%= @page.name %>
   <%= @page.content %> <!-- or whatever data you have for page -->
    # since here we are showing a singular page, we can just use our @page instance variable to list out the sliders
   <% @page.sliders do |s| %>
   ...
   Slider listing code goes here
   ...
   <% end %>


   # pages#show_sliders
   <!-- this is identical to the page#show view, minus the actual page info, and with the addition of a link back to the parent page -->
   <%= link_to "Back to page", page(s.page_id) %>

   <% @page.sliders do |s| %>
   ...
   Slider listing code goes here
   <!-- you can link to any path from the slider listing -->

   <%= link_to "Show", slider(s.id) %>
   <%= link_to "Edit", edit_slider_path(s.id) %>
   <%= link_to "Delete", delete_slider_path(s.id) %>
   ...
   <% end %>


#######################UPDATE#############################
# to define one slider per controller action
class PagesController < ApplicationController
def index
    @pages = Page.all
    # you need to add a "controller_action" column to your Slider model
    @slider = Slider.find_where(controller_action: "pages#index")
end

def show
    @page = Page.find(params[:id])
    @slider = Slider.find_where(controller_action: "pages#show")
end
# etc ...