2015-10-14 43 views
2

我可能試圖爲我的第一個網站太難,但我想在(引導)導航欄上的下拉菜單是靈活的,並顯示保存的工作類別的名稱。在應用程序中使用控制器變量html.erb

這是我試過在application.html.erb文件來完成:

<ul class="dropdown-menu"> 
    <% @workcategory.each do |workcategory| %> 
    <li><%= workcategory.name%></li> 
    <% end %> 

錯誤undefined method每個」失敗,零:NilClass on the <%@ workcategory.each做| workcategory | %>`線。

這是workcategories控制器:

class WorkcategoriesController < ApplicationController 
    before_action :find_workcategory, only: [:edit, :update, :destroy] 
    def index 
     @workcategories = Workcategory.all.order("created_at DESC") 
    end 
    def new 
     @workcategory = Workcategory.new 
    end 
    def create 
     @workcategory = Workcategory.new(post_params) 
     if @workcategory.save 
      flash[:notice] = "Workcategory created" 
      redirect_to(:action=>'index', :workcategory_id => @workcategory.id) 
     else 
      @workcategories = Workcategories.order() 
      render('new') 
     end 
    end 

    def edit 
    end 

    def update 
    end 

    def destroy 
     @workcategory.destroy 
     redirect_to workcategory_path 
    end 
    private 
    def find_workcategory 
     @workcategory=Workcategory.find(params[:id]) 
    end 
    def post_params 
     params.require(:workcategory).permit(:name) 
    end 
end 

任何提示和幫助,歡迎,甚至不相關的最初的問題:)只是謝謝

+0

您想在哪個行動中使用工人類別? –

+0

那麼這就是問題所在。沒有任何操作,我希望它在application.html.erb中。我已經四處尋找其他一些建議使用before_filter的主題。但那並沒有奏效。所以現在我卡住了...... –

回答

3

如果你想在所有你的行動,這是明智的,把它放在你的application_controller.rb

before_filter :set_work_categories 

def set_work_categoriers 
    @w_categories = Workcategory.all.order("created_at DESC") 
end 

這應該很好。

另外,小費。

您可以使用default_scope {order(created_at: :desc)}在模型WorkCategory.rb

然後你可以使用此類似,

def set_work_categoriers 
    @w_categories = Workcategory.all 
end 

我將建議改變變量名@w_categories否則將與您@work_categories名衝突在index行動。

在你application.html.erb文件,更改

<% unless @w_categories.nil? %> 
    <ul class="dropdown-menu"> 
    <% @w_categories.each do |workcategory| %> 
     <li><%= workcategory.name%></li> 
    <% end %> 
    </ul> 
<%end> 

我想這應該做的伎倆

+0

這給了我'未定義的方法'首先爲零:NilClass'。我已經刪除了這條線,並回到零級。等等...我想我可能不會保存它的權利或東西.... –

+0

我添加了change.Just作爲確認,你有'Workcategory'模型中的數據? –

+0

嘿是的謝謝你的除非。我試過了,儘管它不像我想要的那樣工作。現在我/我們知道工作類別沒有正確保存。當我到rails控制檯並檢查時,我有3個工作類別,但他們只是不顯示...我試過了before_filter,但仍然沒有。有任何想法嗎 ? –

2

如果我們談論index行動,那麼你忘記使用合適的變量:

<ul class="dropdown-menu"> 
    <% @workcategories.each do |workcategory| %> 
    <li><%= workcategory.name%></li> 
<% end %> 

更新

如果你想擁有這一切行動,然後初始化@workcategoriesbefore_action

# your_controller.rb 
before_action :initialize_work_categories 
def initialize_work_categories 
    @workcategories = Workcategory.all.order("created_at DESC") 
end 
+0

這給了我同樣的錯誤。我實際上在之前嘗試過'workcategories',並且它沒有工作:)所以問題在其他地方。謝謝。 –

+0

嘿,我已經嘗試了第二個選項,更新。而且這也不起作用。我得到同樣的錯誤。 –

+0

@alexlug,請更新您嘗試使用的確切代碼的答案(包括本'before_action') – nsave

0

你可以讓一個輔助方法work_categories並使用它。無論是在application_helper直接定義它 - 或者,如果你不想使用助手,你可以在你看來把這個代碼在你的ApplicationController

class ApplicationController < ActionController::Base 

    def work_categories 
    Workcategory.all.order("created_at DESC") 
    end 
    helper_method :work_categories 

end 

然後:

<ul class="dropdown-menu"> 
    <% work_categories.each do |workcategory| %> 
    <li><%= workcategory.name%></li> 
    <% end %> 
</ul> 
+0

你好。我試過這個,它拋出:未定義的方法'helper_method'爲主:對象 –

+0

您是否已將代碼添加到apps/controllers/application_controller.rb? – ReggieB

+0

是的....它拋出未定義的方法helper_method錯誤...:/ –

2

佈局

application.html.erb佈局,這意味着它也將存在,無論無論您是否使用Workcategories控制器。

如果要將變量加載到佈局,無論調用哪個控制器,都需要確保@workcategory變量存在。

要做到這一點,你會通常把@workcategory申報到ApplicationController(其中大多數其他控制器繼承):

#app/controllers/application_controller.rb 
class ApplicationController < ActionController::Base 
    before_action :set_categories 

    private 

    def set_categories 
     @workcategory = .... 
    end 
end 

這是爲了填充版面端變量的標準方法。如果您的控制器繼承自Application控制器,則效率不高,只有有效。


一些進一步的指針將包括以下:

1.對象取向

紅寶石,並且憑藉,導軌,是object orientated

這意味着你做的一切都應該圍繞對象旋轉。這就是爲什麼Rails有許多似乎「神奇地」工作的助手。

它們根本就不是魔術 - 它們只需將對象傳遞給它們,然後構建預先燒製的HTML以提供特定的功能。

一個很好的例子是routes

#config/routes.rb 
resources :controller 

enter image description here

的原因,這是非常重要的是,當你打電話的動作/變量,你需要把它們當作對象。這對於新手來說很難做到,但是如果你能夠掌握它的話,它可以大量地幫助你編碼。

-

2.控制器

從上面繼,你要記住,你的控制器真的只是一個操作對象的一種方式。

因此,如果您打電話給@workcategory,您需要了解對象將從哪裏來以及它將如何填充。

+1

謝謝..這是缺少的一塊,before_action與Amit的代碼結合!謝謝 ! –

+0

不錯,如果他們幫忙的話,你應該加倍努力 –

相關問題