2013-06-04 44 views
2

我有3個模型費用,收入和利潤。這些模型屬於房地產,因此房地產有一項開支收入和利潤。房地產屬於用戶,使用戶有很多地產。那麼,我應該在指數中寫什麼,並創建費用,收入和利潤控制器的方法?因此,只顯示那些與特定房地產相關的模型。 (比如有一個由devise提供的current_user方法,在這種情況下,我使用用戶@estates = Estate.where(:user_id => current_user.id)關聯遺產)如何訪問關聯的導軌模型

所以我必須創建新的current_expense,current_revenue和current_profit方法?我如何創建它以及在哪裏。

回答

2

如何有關費用指數html.erb

current_user.estates.each do |estate| 
    estate.expenses.each do |expense| 
    expense.value 
    end 
end 

爲創建行動我喜歡用這裏的寶石simple_form是它的一個railscast:http://railscasts.com/episodes/234-simple-form

,這是我做的:

new.html.erb

<%= simple_form_for [@estate,@expense] do |f| %> 

     <%= f.input :value1 %> 
     <%= f.input :value2 %> 

<% end %> 

控制器

def create 
    respond_to do |format|  
     if @expense.save 
     format.html { redirect_to @expense, notice: '@expense was successfully created.' } 
     else 
     format.html { render action: "new" } 
     end 
    end 
    end 
+0

我不這麼認爲,你有我的問題。也許我解釋得不好。當我與任何用戶登錄時,該用戶可以看到所有收入支出或淨利潤。但我希望那個用戶應該能夠看到只屬於他的房產的收入支出或淨利潤。我能做到這一點嗎? –

+0

多數民衆贊成在我的理解......當前用戶將只能夠看到有關他的屋苑的信息在這裏是魔術發生的地方: current_user.estates.each do | estate | –

+0

但是在節省費用的時候,在費用表中怎麼把estate_id這個外鍵的值? –