如何 link 模型到控制器动作
How to link model to controller action
我的项目中有一个 Slider 模型,它与产品、制造商、文章等其他模型有很多多态关联。
因此,当我对其中一个模型使用 'show' 操作时,我还会显示相关的滑块。没关系。但有时我需要用 'index' action.
显示 Slider
什么是 link 一些滑块到动作而不是其他模型的最佳方法?
更新
路线
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
所以在 'show' 操作中,我只使用 product.slider
来获取相关的 Slider 实例。但我想通过索引操作为所有产品显示另一个滑块。
在那种情况下,您的尝试是不可能的。您不能创建与 控制器操作 的关系。您需要做的是 link 关系的 控制器操作 ,而不是尝试创建与 控制器操作 的关系。一个 model
只能与另一个 model
相关(你不能 has_many index
、show
、delete
、etc
...)- 在其他的话,调出关系的数据,然后 link 到视图中那个关系的 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 ...
我的项目中有一个 Slider 模型,它与产品、制造商、文章等其他模型有很多多态关联。
因此,当我对其中一个模型使用 'show' 操作时,我还会显示相关的滑块。没关系。但有时我需要用 'index' action.
显示 Slider什么是 link 一些滑块到动作而不是其他模型的最佳方法?
更新
路线
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
所以在 'show' 操作中,我只使用 product.slider
来获取相关的 Slider 实例。但我想通过索引操作为所有产品显示另一个滑块。
在那种情况下,您的尝试是不可能的。您不能创建与 控制器操作 的关系。您需要做的是 link 关系的 控制器操作 ,而不是尝试创建与 控制器操作 的关系。一个 model
只能与另一个 model
相关(你不能 has_many index
、show
、delete
、etc
...)- 在其他的话,调出关系的数据,然后 link 到视图中那个关系的 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 ...