2016-01-01 72 views
-1

我試圖拉從Instagram的API圖片在我的Rails應用程序,我目前得到一個錯誤:獲得零級錯誤在我的Rails應用程序

undefined local variable or method 'search' 

我的觀點

<div class="container"> 
     <div class="row"> 
      <% search.each do |instagram| %> 
      <%= image_tag instagram.images.low_resolution.url %> 
      </div> 
      <% end %> 
     </div> 

我的控制器

class PagesController < ApplicationController 

    def home 
    access_token = "hidden" 
    client = Instagram.client(access_token: access_token) 
    default_search = client.tag_search('travel') 
    search_query = client.tag_search(params[:q]) 

    if params[:q] 
     search_query = client.tag_search(params[:q]) 
     @tag = search_query.present? ? search_query : default_search 
    else 
     @tag = tag.first.name 
     @search = client.tag_recent_media(@tag) 
    end 

end 
    def about 
    end 
end 
+0

錯誤在您的視圖中。搜索定義在哪裏? –

+0

不應該在您的視圖中搜索「@ tag」嗎? –

+0

是的錯誤是在我看來... – westman2222

回答

0

試試這個

鑑於

<div class="container"> 
    <div class="row"> 
    <% @search.each do |instagram| %> 
     <%= image_tag instagram.images.low_resolution.url %> 
    <% end %> 
    </div> 
</div> 

控制器

class PagesController < ApplicationController 
    def home 
    access_token = "hidden" 
    client = Instagram.client(access_token: access_token) 
    default_search = client.tag_search('travel') 
    search_query = client.tag_search(params[:q]) 
    if params[:q] 
    search_query = client.tag_search(params[:q]) 
    @tag = search_query.present? ? search_query : default_search 
    @search = client.tag_recent_media(@tag) 
    else 
     @tag = default_search.first.name 
     @search = client.tag_recent_media(@tag) 
    end 
    end 
end 
+0

好的,謝謝..我會盡快嘗試 – westman2222

+0

確定獲取未定義的局部變量或方法'標記' – westman2222

+0

我認爲如果參數[:q]不存在,那麼'@tag = default_search'對? –

0

搜索永遠不會在您的控制器中定義...我認爲您的意思是@tag,因爲這是在您的控制器中定義的,可能意味着您的觀點?

試着改變你的看法,以這樣的:

<div class="container"> 
    <div class="row"> 
     <% @tag.each do |instagram| %> 
     <%= image_tag instagram.images.low_resolution.url %> 
     </div> 
     <% end %> 
    </div> 
+0

好吧,那麼我需要改變以獲得這個啓動? – westman2222

+0

更新了我的控制器,見上面但仍然有相同的問題...... – westman2222

+0

請回到您的原始控制器,並瞭解以@開頭的變量(即:@ tag')和啓動變量沒有(即:'tag')都是不同的變量。 –

0

控制器:

class PagesController < ApplicationController 
    def home 
    access_token = "token" 
    client = Instagram.client(access_token: access_token) 
    default_search = client.tag_search('travel') 


    if params[:q] 
     search_query = client.tag_search(params[:q]) 
     @tag = search_query.present? ? search_query : default_search 

    else 
     @tag = default_search 
    end 

    @tag = @tag.first.name 
    @search = client.tag_recent_media(@tag) 



    end 

    def about 
    end 

end 
相關問題