2016-07-23 69 views
0

在我的靜態頁面home.html.erb方法`個人資料」的紅寶石,我有一個StaticPagesController參考。我相信我正確地做這個,但是仍然得到未定義的局部變量或方法`個人資料」。未定義的局部變量或on Rails的

StaticPagesController

class StaticPagesController < ApplicationController 

    def profile 
     redirect_to profile_path(current_user) 
    end 
end 

home.html.erb

<% if logged_in? %> 
    <% profile %> 
<% else %> 
    <h1>Welcome to myProjects</h1> 
    <%= link_to "Sign up!", signup_path, class: "btn btn-lg btn-primary" %> 
<% end %> 

+0

Pragash答案是正確的,但究竟什麼是你想怎麼辦? – jonathanrz

回答

0

你需要讓def profile的helper方法,因此這種方法將可用於查看。只需添加這條線之下您def profile方法:

helper_method :profile 

你最終的類將是這樣的:

class StaticPagesController < ApplicationController 

    def profile 
     redirect_to profile_path(current_user) 
    end 

    helper_method :profile 
    end 
0

嗯,我認爲你正試圖訪問從視圖控制器中定義的方法。我很抱歉,但這不可能。使用MVC,你不能像這樣觸發控制器中的方法。理想情況下,你應該在應用程序/傭工/ application_helper.rb或應用程序/傭工/ static_pages_helper.rb下定義一個輔助方法。試圖訪問它,你正在做會導致你看到的錯誤的方式。

相關問題