控制器中的Rails Helper方法适用于所有页面

时间:2018-03-11 21:24:59

标签: ruby-on-rails ruby ruby-on-rails-5

我的页脚中有一个Instagram Feed。当我在我的控制器中插入适用于我的视图的方法时,它只显示在第一页(见下文)

    class CreativesController < ApplicationController
  layout "creative"



  def index
    **@instagram = Instagram.user_recent_media(:count => 1)**
    @campaigns = storage.list_for(params[:page], params[:tag])
    @campaigned = Campaign.order("created_at DESC").limit(6)
    @recent = Campaign.order("created_at DESC").limit(3)

    end

    # GET /posts/1
    # GET /posts/1.json
  def show

    @campaign = storage.friendly.find(params[:id])
    @gallery = storage.friendly.find(params[:id])
  end

  def gallery
   @galleries = storage.list_for(params[:page], params[:tag])
    @galleried = Gallery.order("created_at DESC")
    @current = Gallery.order("created_at DESC")

  end


  private

  def storage
    Campaign.published
    Gallery.published

  end







end

我得到以下内容:

first photo

现在当我导航到另一个页面时,我收到以下错误:

second photo

我想要做什么并且遇到问题是将instagram方法应用于应用程序控制器以将其应用于所有页面,以便为页脚中的所有页面加载而不会给我错误。

以下是我的application.controller现在的样子:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  layout 'author'
 before_action :load_posts
 def instagram
    @instagram = Instagram.user_recent_media(:count => 1)
 end

private

  def load_posts
    @posts = Post.order("created_at DESC").limit(3)
  end

end

我仍然遇到同样的错误。

这就是我的页脚代码:

<!-- Latest -->
                        <div class="col-md-3 md-margin-bottom-40">
                            <div class="posts">
                                <div class="headline"><h2>Latest Instagram Post</h2></div>
                            <% @instagram.each do |instagram| %>

              <%= image_tag instagram.images.standard_resolution.url, class: "img-responsive" %>

                <%= instagram.caption.text %>


            <%end%>
                            </div>
                        </div>
                        <!-- End Latest -->

这就是我的路线:

 get 'contacts/index'

  get 'document/download'
  get 'confirmation/confirmation'
  get 'document/download'
  get 'show' => 'maps#show_image'

  root 'creatives#index'
  get '/about' => 'creatives#about'
  get '/news' => 'creatives#news'
  get '/careers' => 'creatives#careers'
  get '/contacts' => 'creatives#contacts'
  get '/gallery' => 'creatives#gallery'
  get '/geopath' => 'creatives#geopath'
  get '/steps' => 'creatives#steps'
  get '/browse' => 'maps#show'
  get '/search' => 'maps#index'
  get '/design' => 'creatives#design'
  post "favorite", to: "maps#favorite"
  post "unfavorite", to: "maps#unfavorite"

更新2

我更改了我的广告素材控制器并添加了以下内容,除了一些使用不同控制器的其他页面外,它还可以正常工作。现在它以这种方式工作,但我试图看看是否有一种更简单的方法可以跨所有页面应用它而无需反复重复代码。这是我认为我需要在应用程序方法中添加一些内容的地方,但我无法弄明白。

class CreativesController < ApplicationController
  layout "creative"


def about
@instagram = Instagram.user_recent_media(:count => 1)
end

def careers
@instagram = Instagram.user_recent_media(:count => 1)
end

def contacts
@instagram = Instagram.user_recent_media(:count => 1)
end

def design
@instagram = Instagram.user_recent_media(:count => 1)
end

def geopath
@instagram = Instagram.user_recent_media(:count => 1)
end

def news
@instagram = Instagram.user_recent_media(:count => 1)
end

def steps
@instagram = Instagram.user_recent_media(:count => 1)
end

  def index
    @instagram = Instagram.user_recent_media(:count => 1)
    @campaigns = storage.list_for(params[:page], params[:tag])
    @campaigned = Campaign.order("created_at DESC").limit(6)
    @recent = Campaign.order("created_at DESC").limit(3)

    end

    # GET /posts/1
    # GET /posts/1.json
  def show
@instagram = Instagram.user_recent_media(:count => 1)
    @campaign = storage.friendly.find(params[:id])
    @gallery = storage.friendly.find(params[:id])
  end

  def gallery
    @instagram = Instagram.user_recent_media(:count => 1)
   @galleries = storage.list_for(params[:page], params[:tag])
    @galleried = Gallery.order("created_at DESC")
    @current = Gallery.order("created_at DESC")

  end


  private

  def storage
    Campaign.published
    Gallery.published

  end







end

我得到了正确的答案我刚刚将我提供的代码添加到我的应用程序控制器中,以便在所有视图中应用它。

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  layout 'author'
 before_action :load_posts
  before_action :set_instagram


private
 def set_instagram
    @instagram = Instagram.user_recent_media(count: 1)
  end
  def load_posts
    @posts = Post.order("created_at DESC").limit(3)
  end

end

1 个答案:

答案 0 :(得分:1)

对于重复的情况,您可以使用before_action回调。

class CreativesController < ApplicationController
  layout 'creative'
  before_action :set_instagram

  ...

  private

  def set_instagram
    @instagram = Instagram.user_recent_media(count: 1)
  end
end 

您可以将onlyexcept选项与符号数组一起使用,其中符号是每个操作的名称,具体取决于您之前要设置该实例变量的操作。如果您想在每个操作之前设置它,那么您省略任何选项。