简体   繁体   English

Rails:如何使对象在所有视图中可用?

[英]Rails: How can I make an object available in all views?

I have a search object which is created in my Galleries controller and displayed in my Galleries View: 我有一个在我的Galleries控制器中创建并显示在我的Galleries视图中的search对象:

app/controllers/galleries_controller.rb 应用程序/控制器/ galleries_controller.rb

class GalleriesController < ApplicationController
  def index
    @galleries = Gallery.all
    @search = Search.new
  end

This object represents a search bar. 该对象代表搜索栏。 I want to move the search bar into my layouts/application.html.erb view and make it available on all pages in the header. 我想将搜索栏移到我的layouts/application.html.erb视图中,并使其在标题的所有页面上可用。 This would require me to make the search object globally available, and I am not sure how to do that. 这将要求我使search对象全局可用,但我不确定如何做到这一点。 I tried sticking in ApplicationController and thought that would make it available in all views since everything inherence from there, but it didn't work. 我尝试坚持使用ApplicationController并认为这将使它在所有视图中都可用,因为所有继承都源于此,但它没有用。 How can I make an object available in all views? 如何使对象在所有视图中可用?

You can do it with a before_action (aka. before_filter ) in the ApplicationController , from which all your controllers should inherit. 你可以用做before_action (亦称before_filter中) ApplicationController ,从所有的控制器应该继承。

class ApplicationController < ActionController::Base
  before_action :make_search

  def make_search
    @galleries = Gallery.all
    @search = Search.new
  end
end

This will make the function run before every action. 这将使函数在执行每个操作之前运行。

You can use skip_before_action :make_search to disable it in a specific controller, if you need to, or skip_before_action :make_search, only: :index to disable it for only the index action for a controller. 如果需要,可以使用skip_before_action :make_search在特定控制器中禁用它,或者使用skip_before_action :make_search, only: :index仅对控制器的index操作禁用它。

If you only want this for a few controllers, you could define make_search in the ApplicationController , and put the before_action :make_search in the controllers for which you want to enable it... 如果只需要几个控制器,可以在ApplicationController定义make_search ,然后将before_action :make_search放入要启用它的控制器中。

Add this to your ApplicationController : 将此添加到您的ApplicationController

def search
  @search ||= Search.new
end

def galleries
  @galleries ||= Gallery.all
end

You then use the function ( search ) in your views instead if the instance variable @search ); 然后,如果实例变量@search ),则在视图中使用该函数( search ); the ||= makes sure the code is executed only once (thus saving database queries). ||=确保代码仅执行一次(从而节省了数据库查询)。

You can make the object globally available by breaking MVC and just create the object inside the view: 您可以通过破坏MVC使对象在全局范围内可用,而只需在视图内部创建对象即可:

    <% @search = Search.new %>
    <%= form_for @search do |form| %>
      <%= form.text_field :query, placeholder: "Search for a Picture" %>
    <% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:对基础表进行更改后,如何重新生成脚手架创建的所有视图? - Rails: How can I regenerate all the views created by scaffold after I make changes to the underlying table? 我如何在Rails应用程序中使基础全局变量可用于我的所有scss文件 - How can i make foundation global variables available to all my scss files in a rails app Rails3 - 如何使控制器和视图可以使用自定义助手 - Rails3 – How do I make a custom Helper available to controllers And Views 如何在Rails中为多个模型提供相同的方法? - How can I make the same method available to multiple models in Rails? 如何在所有视图中看到变量 - rails - how to make a variable seen in all views - rails 如何在rails中全局使用class \\ object? - How to make class\object available globally in rails? 如何在版面上呈现设计登录和注册视图,以在所有页面上可用 - How can I render the devise signin and signup views on the layout to be available on all pages 如何使gem中的功能可用于Sinatra视图? - How do I make functions in a gem available to Sinatra views? 如何让所有表包含Rails 4中的某些列? - How can I make all tables contain certain columns in Rails 4? 如何在Rails插件中测试视图? - How can I test views in a Rails plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM